1 /* Copyright (c) 2023 Google LLC
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include <zephyr/dsp/print_format.h>
6 #include <zephyr/ztest.h>
7 #include <string.h>
8 
9 #define float_multiplier(type) ((INT64_C(1) << (8 * sizeof(type) - 1)) - 1)
10 
11 #define assert_strings(expected, actual)                                                           \
12 	zexpect_equal(strlen(expected), strlen(actual), "Expected %d(%s), got %d(%s)",             \
13 		      strlen(expected), expected, strlen(actual), actual);                         \
14 	zexpect_mem_equal(expected, actual, MIN(strlen(expected), strlen(actual)),                 \
15 			  "Expected '%s', got '%s'", expected, actual)
16 
17 ZTEST_SUITE(zdsp_print_format, NULL, NULL, NULL, NULL, NULL);
18 
ZTEST(zdsp_print_format,test_print_q31_precision_positive)19 ZTEST(zdsp_print_format, test_print_q31_precision_positive)
20 {
21 	char buffer[256];
22 	q31_t q = (q31_t)0x0f5c28f0; /* 0.119999997 */
23 
24 	snprintf(buffer, 256, "%" PRIq(6), PRIq_arg(q, 6, 0));
25 	assert_strings("0.119999", buffer);
26 
27 	snprintf(buffer, 256, "%" PRIq(6), PRIq_arg(q, 6, 1));
28 	assert_strings("0.239999", buffer);
29 
30 	snprintf(buffer, 256, "%" PRIq(6), PRIq_arg(q, 6, -2));
31 	assert_strings("0.029999", buffer);
32 
33 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 0));
34 	assert_strings("0.1199", buffer);
35 
36 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 1));
37 	assert_strings("0.2399", buffer);
38 
39 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, -2));
40 	assert_strings("0.0299", buffer);
41 }
42 
ZTEST(zdsp_print_format,test_print_q31_precision_negative)43 ZTEST(zdsp_print_format, test_print_q31_precision_negative)
44 {
45 	char buffer[256];
46 	q31_t q = (q31_t)0x83d70a00; /* -0.970000029 */
47 
48 	snprintf(buffer, 256, "%" PRIq(6), PRIq_arg(q, 6, 0));
49 	assert_strings("-0.970000", buffer);
50 
51 	snprintf(buffer, 256, "%" PRIq(6), PRIq_arg(q, 6, 1));
52 	assert_strings("-1.940000", buffer);
53 
54 	snprintf(buffer, 256, "%" PRIq(6), PRIq_arg(q, 6, -2));
55 	assert_strings("-0.242500", buffer);
56 
57 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 0));
58 	assert_strings("-0.9700", buffer);
59 
60 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 1));
61 	assert_strings("-1.9400", buffer);
62 
63 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, -2));
64 	assert_strings("-0.2425", buffer);
65 }
66 
ZTEST(zdsp_print_format,test_print_q15_precision_positive)67 ZTEST(zdsp_print_format, test_print_q15_precision_positive)
68 {
69 	char buffer[256];
70 	q15_t q = (q15_t)0x28f5; /* 0.319986672 */
71 
72 	snprintf(buffer, 256, "%" PRIq(5), PRIq_arg(q, 5, 0));
73 	assert_strings("0.31997", buffer);
74 
75 	snprintf(buffer, 256, "%" PRIq(5), PRIq_arg(q, 5, 1));
76 	assert_strings("0.63995", buffer);
77 
78 	snprintf(buffer, 256, "%" PRIq(5), PRIq_arg(q, 5, -2));
79 	assert_strings("0.07999", buffer);
80 
81 	snprintf(buffer, 256, "%" PRIq(3), PRIq_arg(q, 3, 0));
82 	assert_strings("0.319", buffer);
83 
84 	snprintf(buffer, 256, "%" PRIq(3), PRIq_arg(q, 3, 1));
85 	assert_strings("0.639", buffer);
86 
87 	snprintf(buffer, 256, "%" PRIq(3), PRIq_arg(q, 3, -2));
88 	assert_strings("0.079", buffer);
89 }
90 
ZTEST(zdsp_print_format,test_print_q15_precision_negative)91 ZTEST(zdsp_print_format, test_print_q15_precision_negative)
92 {
93 	char buffer[256];
94 	q15_t q = (q15_t)0xc4bd; /* -0.462965789 */
95 
96 	snprintf(buffer, 256, "%" PRIq(5), PRIq_arg(q, 5, 0));
97 	assert_strings("-0.46298", buffer);
98 
99 	snprintf(buffer, 256, "%" PRIq(5), PRIq_arg(q, 5, 1));
100 	assert_strings("-0.92596", buffer);
101 
102 	snprintf(buffer, 256, "%" PRIq(5), PRIq_arg(q, 5, -2));
103 	assert_strings("-0.11574", buffer);
104 
105 	snprintf(buffer, 256, "%" PRIq(3), PRIq_arg(q, 3, 0));
106 	assert_strings("-0.462", buffer);
107 
108 	snprintf(buffer, 256, "%" PRIq(3), PRIq_arg(q, 3, 1));
109 	assert_strings("-0.925", buffer);
110 
111 	snprintf(buffer, 256, "%" PRIq(3), PRIq_arg(q, 3, -2));
112 	assert_strings("-0.115", buffer);
113 }
114 
ZTEST(zdsp_print_format,test_print_q7_precision_positive)115 ZTEST(zdsp_print_format, test_print_q7_precision_positive)
116 {
117 	char buffer[256];
118 	q7_t q = (q7_t)0x01; /* 0.007874016 */
119 
120 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 0));
121 	assert_strings("0.0078", buffer);
122 
123 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 1));
124 	assert_strings("0.0156", buffer);
125 
126 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, -2));
127 	assert_strings("0.0019", buffer);
128 
129 	snprintf(buffer, 256, "%" PRIq(2), PRIq_arg(q, 2, 0));
130 	assert_strings("0.00", buffer);
131 
132 	snprintf(buffer, 256, "%" PRIq(2), PRIq_arg(q, 2, 1));
133 	assert_strings("0.01", buffer);
134 
135 	snprintf(buffer, 256, "%" PRIq(2), PRIq_arg(q, 2, -2));
136 	assert_strings("0.00", buffer);
137 }
138 
ZTEST(zdsp_print_format,test_print_q7_precision_negative)139 ZTEST(zdsp_print_format, test_print_q7_precision_negative)
140 {
141 	char buffer[256];
142 	q7_t q = (q7_t)0xe2; /* -0.228346457 */
143 
144 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 0));
145 	assert_strings("-0.2343", buffer);
146 
147 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, 1));
148 	assert_strings("-0.4687", buffer);
149 
150 	snprintf(buffer, 256, "%" PRIq(4), PRIq_arg(q, 4, -2));
151 	assert_strings("-0.0585", buffer);
152 
153 	snprintf(buffer, 256, "%" PRIq(2), PRIq_arg(q, 2, 0));
154 	assert_strings("-0.23", buffer);
155 
156 	snprintf(buffer, 256, "%" PRIq(2), PRIq_arg(q, 2, 1));
157 	assert_strings("-0.46", buffer);
158 
159 	snprintf(buffer, 256, "%" PRIq(2), PRIq_arg(q, 2, -2));
160 	assert_strings("-0.05", buffer);
161 }
162