unit-tests: Added bliss_fft_speed test
[strongswan.git] / src / libstrongswan / plugins / bliss / tests / suites / test_bliss_fft.c
1 /*
2 * Copyright (C) 2014-2016 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <bliss_fft.h>
19
20 #include <time.h>
21
22 static bliss_fft_params_t *fft_params[] = {
23 &bliss_fft_17_8,
24 &bliss_fft_12289_512
25 };
26
27 START_TEST(test_bliss_fft_impulse)
28 {
29 bliss_fft_t *fft;
30 uint16_t n = fft_params[_i]->n;
31 uint32_t x[n], X[n];
32 int i;
33
34 for (i = 0; i < n; i++)
35 {
36 x[i] = 0;
37 }
38 x[0] = 1;
39
40 fft = bliss_fft_create(fft_params[_i]);
41 fft->transform(fft, x, X, FALSE);
42
43 for (i = 0; i < n; i++)
44 {
45 ck_assert(X[i] == 1);
46 }
47 fft->transform(fft, X, x, TRUE);
48
49 for (i = 0; i < n; i++)
50 {
51 ck_assert(x[i] == (i == 0));
52 }
53 fft->destroy(fft);
54 }
55 END_TEST
56
57 START_TEST(test_bliss_fft_wrap)
58 {
59 bliss_fft_t *fft;
60 uint16_t n = fft_params[_i]->n;
61 uint16_t q = fft_params[_i]->q;
62 uint32_t x[n],y[n], X[n], Y[n];
63 int i, j;
64
65 for (i = 0; i < n; i++)
66 {
67 x[i] = i;
68 y[i] = 0;
69 }
70 fft = bliss_fft_create(fft_params[_i]);
71 ck_assert(fft->get_size(fft) == n);
72 ck_assert(fft->get_modulus(fft) == q);
73 fft->transform(fft, x, X, FALSE);
74
75 for (j = 0; j < n; j++)
76 {
77 y[j] = 1;
78 fft->transform(fft, y, Y, FALSE);
79
80 for (i = 0; i < n; i++)
81 {
82 Y[i] = (X[i] * Y[i]) % q;
83 }
84 fft->transform(fft, Y, Y, TRUE);
85
86 for (i = 0; i < n; i++)
87 {
88 ck_assert(Y[i] == ( i < j ? q - n - i + j : i - j));
89 }
90 y[j] = 0;
91 }
92 fft->destroy(fft);
93 }
94 END_TEST
95
96 START_TEST(test_bliss_fft_speed)
97 {
98 bliss_fft_t *fft;
99 struct timespec start, stop;
100 uint16_t n = bliss_fft_12289_512.n;
101 uint32_t x[n], X[n];
102 int i, m, count = 100000;
103
104 for (i = 0; i < n; i++)
105 {
106 x[i] = i;
107 }
108 fft = bliss_fft_create(&bliss_fft_12289_512);
109
110 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start);
111 for (m = 0; m < count; m++)
112 {
113 fft->transform(fft, x, X, FALSE);
114 fft->transform(fft, X, x, TRUE);
115 }
116 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop);
117
118 DBG0(DBG_LIB, "%d FFT loops in %d ms\n", count,
119 (stop.tv_nsec - start.tv_nsec) / 1000000 +
120 (stop.tv_sec - start.tv_sec) * 1000);
121
122 for (i = 0; i < n; i++)
123 {
124 ck_assert(x[i] == i);
125 }
126 fft->destroy(fft);
127 }
128 END_TEST
129
130 Suite *bliss_fft_suite_create()
131 {
132 Suite *s;
133 TCase *tc;
134
135 s = suite_create("bliss_fft");
136
137 tc = tcase_create("impulse");
138 tcase_add_loop_test(tc, test_bliss_fft_impulse, 0, countof(fft_params));
139 suite_add_tcase(s, tc);
140
141 tc = tcase_create("negative_wrap");
142 tcase_add_loop_test(tc, test_bliss_fft_wrap, 0, countof(fft_params));
143 suite_add_tcase(s, tc);
144
145 tc = tcase_create("speed");
146 tcase_set_timeout(tc, 10);
147 tcase_add_test(tc, test_bliss_fft_speed);
148 suite_add_tcase(s, tc);
149
150 return s;
151 }