1 // SPDX-License-Identifier: BSD-3-Clause
2 //
3 // Copyright(c) 2018 Intel Corporation. All rights reserved.
4 //
5 // Author: Marcin Maka <marcin.maka@linux.intel.com>
6 //         Janusz Jankowski <janusz.jankowski@linux.intel.com>
7 
8 #include <sof/math/numbers.h>
9 
10 #include <stdarg.h>
11 #include <stddef.h>
12 #include <setjmp.h>
13 #include <stdint.h>
14 #include <cmocka.h>
15 
test_math_numbers_gcd_for_5083_and_391_equals_391(void ** state)16 static void test_math_numbers_gcd_for_5083_and_391_equals_391(void **state)
17 {
18 	int r;
19 
20 	(void)state;
21 
22 	r = gcd(5083, 391);
23 	assert_int_equal(r, 391);
24 }
25 
test_math_numbers_gcd_for_12_and_9_equals_3(void ** state)26 static void test_math_numbers_gcd_for_12_and_9_equals_3(void **state)
27 {
28 	int r;
29 
30 	(void)state;
31 
32 	r = gcd(12, 9);
33 	assert_int_equal(r, 3);
34 }
35 
test_math_numbers_gcd_for_5_and_0_equals_5(void ** state)36 static void test_math_numbers_gcd_for_5_and_0_equals_5(void **state)
37 {
38 	int r;
39 
40 	(void)state;
41 
42 	r = gcd(5, 0);
43 	assert_int_equal(r, 5);
44 }
45 
test_math_numbers_gcd_for_0_and_5_equals_5(void ** state)46 static void test_math_numbers_gcd_for_0_and_5_equals_5(void **state)
47 {
48 	int r;
49 
50 	(void)state;
51 
52 	r = gcd(0, 5);
53 	assert_int_equal(r, 5);
54 }
55 
test_math_numbers_gcd_for_0_and_0_equals_0(void ** state)56 static void test_math_numbers_gcd_for_0_and_0_equals_0(void **state)
57 {
58 	int r;
59 
60 	(void)state;
61 
62 	r = gcd(0, 0);
63 	assert_int_equal(r, 0);
64 }
65 
test_math_numbers_gcd_for_neg_4_and_14_equals_2(void ** state)66 static void test_math_numbers_gcd_for_neg_4_and_14_equals_2(void **state)
67 {
68 	int r;
69 
70 	(void)state;
71 
72 	r = gcd(-4, 14);
73 	assert_int_equal(r, 2);
74 }
75 
test_math_numbers_gcd_for_4_and_neg_14_equals_2(void ** state)76 static void test_math_numbers_gcd_for_4_and_neg_14_equals_2(void **state)
77 {
78 	int r;
79 
80 	(void)state;
81 
82 	r = gcd(4, -14);
83 	assert_int_equal(r, 2);
84 }
85 
test_math_numbers_gcd_for_neg_4_and_neg_14_equals_2(void ** state)86 static void test_math_numbers_gcd_for_neg_4_and_neg_14_equals_2(void **state)
87 {
88 	int r;
89 
90 	(void)state;
91 
92 	r = gcd(-4, -14);
93 	assert_int_equal(r, 2);
94 }
95 
main(void)96 int main(void)
97 {
98 	const struct CMUnitTest tests[] = {
99 		cmocka_unit_test
100 			(test_math_numbers_gcd_for_5083_and_391_equals_391),
101 		cmocka_unit_test(test_math_numbers_gcd_for_12_and_9_equals_3),
102 		cmocka_unit_test(test_math_numbers_gcd_for_5_and_0_equals_5),
103 		cmocka_unit_test(test_math_numbers_gcd_for_0_and_5_equals_5),
104 		cmocka_unit_test(test_math_numbers_gcd_for_0_and_0_equals_0),
105 		cmocka_unit_test
106 			(test_math_numbers_gcd_for_neg_4_and_14_equals_2),
107 		cmocka_unit_test
108 			(test_math_numbers_gcd_for_4_and_neg_14_equals_2),
109 		cmocka_unit_test
110 			(test_math_numbers_gcd_for_neg_4_and_neg_14_equals_2)
111 	};
112 
113 	cmocka_set_message_output(CM_OUTPUT_TAP);
114 
115 	return cmocka_run_group_tests(tests, NULL, NULL);
116 }
117