xref: /linux/drivers/iio/test/iio-test-format.c (revision 23c48a124b469cee2eb0c75e6d22d366d1caa118)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Unit tests for IIO formatting functions
3  *
4  * Copyright (c) 2020 Lars-Peter Clausen <lars@metafoo.de>
5  */
6 
7 #include <kunit/test.h>
8 #include <linux/iio/iio.h>
9 
10 #define IIO_TEST_FORMAT_EXPECT_EQ(_test, _buf, _ret, _val) do { \
11 		KUNIT_EXPECT_EQ(_test, strlen(_buf), _ret); \
12 		KUNIT_EXPECT_STREQ(_test, (_buf), (_val)); \
13 	} while (0)
14 
15 static void iio_test_iio_format_value_integer(struct kunit *test)
16 {
17 	char *buf;
18 	int val;
19 	int ret;
20 
21 	buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
22 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
23 
24 	val = 42;
25 	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
26 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "42\n");
27 
28 	val = -23;
29 	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
30 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-23\n");
31 
32 	val = 0;
33 	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
34 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0\n");
35 
36 	val = INT_MAX;
37 	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
38 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "2147483647\n");
39 
40 	val = INT_MIN;
41 	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
42 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-2147483648\n");
43 }
44 
45 static void iio_test_iio_format_value_fixedpoint(struct kunit *test)
46 {
47 	int values[2];
48 	char *buf;
49 	int ret;
50 
51 	buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
52 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
53 
54 	/* positive >= 1 */
55 	values[0] = 1;
56 	values[1] = 10;
57 
58 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, ARRAY_SIZE(values), values);
59 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000010\n");
60 
61 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, ARRAY_SIZE(values), values);
62 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000010 dB\n");
63 
64 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, ARRAY_SIZE(values), values);
65 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000000010\n");
66 
67 	/* positive < 1 */
68 	values[0] = 0;
69 	values[1] = 12;
70 
71 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, ARRAY_SIZE(values), values);
72 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000012\n");
73 
74 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, ARRAY_SIZE(values), values);
75 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000012 dB\n");
76 
77 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, ARRAY_SIZE(values), values);
78 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000012\n");
79 
80 	/* negative <= -1 */
81 	values[0] = -1;
82 	values[1] = 10;
83 
84 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, ARRAY_SIZE(values), values);
85 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000010\n");
86 
87 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, ARRAY_SIZE(values), values);
88 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000010 dB\n");
89 
90 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, ARRAY_SIZE(values), values);
91 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000000010\n");
92 
93 	/* negative > -1 */
94 	values[0] = 0;
95 	values[1] = -123;
96 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, ARRAY_SIZE(values), values);
97 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000123\n");
98 
99 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, ARRAY_SIZE(values), values);
100 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000123 dB\n");
101 
102 	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, ARRAY_SIZE(values), values);
103 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000000123\n");
104 }
105 
106 static void iio_test_iio_format_value_fractional(struct kunit *test)
107 {
108 	int values[2];
109 	char *buf;
110 	int ret;
111 
112 	buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
113 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
114 
115 	/* positive < 1 */
116 	values[0] = 1;
117 	values[1] = 10;
118 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, ARRAY_SIZE(values), values);
119 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.100000000\n");
120 
121 	/* positive >= 1 */
122 	values[0] = 100;
123 	values[1] = 3;
124 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, ARRAY_SIZE(values), values);
125 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "33.333333333\n");
126 
127 	/* negative > -1 */
128 	values[0] = -1;
129 	values[1] = 1000000000;
130 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, ARRAY_SIZE(values), values);
131 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000000001\n");
132 
133 	/* negative <= -1 */
134 	values[0] = -200;
135 	values[1] = 3;
136 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, ARRAY_SIZE(values), values);
137 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-66.666666666\n");
138 
139 	/* Zero */
140 	values[0] = 0;
141 	values[1] = -10;
142 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, ARRAY_SIZE(values), values);
143 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000000\n");
144 }
145 
146 static void iio_test_iio_format_value_fractional_log2(struct kunit *test)
147 {
148 	int values[2];
149 	char *buf;
150 	int ret;
151 
152 	buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
153 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
154 
155 	/* positive < 1 */
156 	values[0] = 123;
157 	values[1] = 10;
158 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, ARRAY_SIZE(values), values);
159 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.120117187\n");
160 
161 	/* positive >= 1 */
162 	values[0] = 1234567;
163 	values[1] = 10;
164 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, ARRAY_SIZE(values), values);
165 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1205.631835937\n");
166 
167 	/* negative > -1 */
168 	values[0] = -123;
169 	values[1] = 10;
170 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, ARRAY_SIZE(values), values);
171 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.120117187\n");
172 
173 	/* negative <= -1 */
174 	values[0] = -1234567;
175 	values[1] = 10;
176 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, ARRAY_SIZE(values), values);
177 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1205.631835937\n");
178 
179 	/* Zero */
180 	values[0] = 0;
181 	values[1] = 10;
182 	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, ARRAY_SIZE(values), values);
183 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000000\n");
184 }
185 
186 static void iio_test_iio_format_value_multiple(struct kunit *test)
187 {
188 	int values[] = {1, -2, 3, -4, 5};
189 	char *buf;
190 	int ret;
191 
192 	buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
193 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
194 
195 	ret = iio_format_value(buf, IIO_VAL_INT_MULTIPLE,
196 			       ARRAY_SIZE(values), values);
197 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1 -2 3 -4 5 \n");
198 }
199 
200 static void iio_test_iio_format_value_integer_64(struct kunit *test)
201 {
202 	int values[2];
203 	s64 value;
204 	char *buf;
205 	int ret;
206 
207 	buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
208 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
209 
210 	value = 24;
211 	values[0] = lower_32_bits(value);
212 	values[1] = upper_32_bits(value);
213 	ret = iio_format_value(buf, IIO_VAL_INT_64, ARRAY_SIZE(values), values);
214 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "24\n");
215 
216 	value = -24;
217 	values[0] = lower_32_bits(value);
218 	values[1] = upper_32_bits(value);
219 	ret = iio_format_value(buf, IIO_VAL_INT_64, ARRAY_SIZE(values), values);
220 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-24\n");
221 
222 	value = 0;
223 	values[0] = lower_32_bits(value);
224 	values[1] = upper_32_bits(value);
225 	ret = iio_format_value(buf, IIO_VAL_INT_64, ARRAY_SIZE(values), values);
226 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0\n");
227 
228 	value = UINT_MAX;
229 	values[0] = lower_32_bits(value);
230 	values[1] = upper_32_bits(value);
231 	ret = iio_format_value(buf, IIO_VAL_INT_64, ARRAY_SIZE(values), values);
232 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "4294967295\n");
233 
234 	value = -((s64)UINT_MAX);
235 	values[0] = lower_32_bits(value);
236 	values[1] = upper_32_bits(value);
237 	ret = iio_format_value(buf, IIO_VAL_INT_64, ARRAY_SIZE(values), values);
238 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-4294967295\n");
239 
240 	value = LLONG_MAX;
241 	values[0] = lower_32_bits(value);
242 	values[1] = upper_32_bits(value);
243 	ret = iio_format_value(buf, IIO_VAL_INT_64, ARRAY_SIZE(values), values);
244 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "9223372036854775807\n");
245 
246 	value = LLONG_MIN;
247 	values[0] = lower_32_bits(value);
248 	values[1] = upper_32_bits(value);
249 	ret = iio_format_value(buf, IIO_VAL_INT_64, ARRAY_SIZE(values), values);
250 	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-9223372036854775808\n");
251 }
252 
253 static struct kunit_case iio_format_test_cases[] = {
254 		KUNIT_CASE(iio_test_iio_format_value_integer),
255 		KUNIT_CASE(iio_test_iio_format_value_fixedpoint),
256 		KUNIT_CASE(iio_test_iio_format_value_fractional),
257 		KUNIT_CASE(iio_test_iio_format_value_fractional_log2),
258 		KUNIT_CASE(iio_test_iio_format_value_multiple),
259 		KUNIT_CASE(iio_test_iio_format_value_integer_64),
260 		{}
261 };
262 
263 static struct kunit_suite iio_format_test_suite = {
264 	.name = "iio-format",
265 	.test_cases = iio_format_test_cases,
266 };
267 kunit_test_suite(iio_format_test_suite);
268 
269 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
270 MODULE_DESCRIPTION("Test IIO formatting functions");
271 MODULE_LICENSE("GPL v2");
272