summaryrefslogtreecommitdiffstats
path: root/drivers/iio/test/iio-test-format.c
blob: 55a0cfe9181d23cdd1543de7ba296c6e327713a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// SPDX-License-Identifier: GPL-2.0-only
/* Unit tests for IIO formatting functions
 *
 * Copyright (c) 2020 Lars-Peter Clausen <lars@metafoo.de>
 */

#include <kunit/test.h>
#include <linux/iio/iio.h>

#define IIO_TEST_FORMAT_EXPECT_EQ(_test, _buf, _ret, _val) do { \
		KUNIT_EXPECT_EQ(_test, (int)strlen(_buf), _ret); \
		KUNIT_EXPECT_STREQ(_test, (_buf), (_val)); \
	} while (0)

static void iio_test_iio_format_value_integer(struct kunit *test)
{
	char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
	int val;
	int ret;

	val = 42;
	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "42\n");

	val = -23;
	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-23\n");

	val = 0;
	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0\n");

	val = INT_MAX;
	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "2147483647\n");

	val = INT_MIN;
	ret = iio_format_value(buf, IIO_VAL_INT, 1, &val);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-2147483648\n");
}

static void iio_test_iio_format_value_fixedpoint(struct kunit *test)
{
	char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
	int values[2];
	int ret;

	/* positive >= 1 */
	values[0] = 1;
	values[1] = 10;

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000010\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000010 dB\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000000010\n");

	/* positive < 1 */
	values[0] = 0;
	values[1] = 12;

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000012\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000012 dB\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000012\n");

	/* negative <= -1 */
	values[0] = -1;
	values[1] = 10;

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000010\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000010 dB\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000000010\n");

	/* negative > -1 */
	values[0] = 0;
	values[1] = -123;
	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000123\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000123 dB\n");

	ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000000123\n");
}

static void iio_test_iio_format_value_fractional(struct kunit *test)
{
	char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
	int values[2];
	int ret;

	/* positive < 1 */
	values[0] = 1;
	values[1] = 10;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.100000000\n");

	/* positive >= 1 */
	values[0] = 100;
	values[1] = 3;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "33.333333333\n");

	/* negative > -1 */
	values[0] = -1;
	values[1] = 1000000000;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000000001\n");

	/* negative <= -1 */
	values[0] = -200;
	values[1] = 3;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-66.666666666\n");

	/* Zero */
	values[0] = 0;
	values[1] = -10;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000000\n");
}

static void iio_test_iio_format_value_fractional_log2(struct kunit *test)
{
	char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
	int values[2];
	int ret;

	/* positive < 1 */
	values[0] = 123;
	values[1] = 10;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.120117187\n");

	/* positive >= 1 */
	values[0] = 1234567;
	values[1] = 10;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1205.631835937\n");

	/* negative > -1 */
	values[0] = -123;
	values[1] = 10;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.120117187\n");

	/* negative <= -1 */
	values[0] = -1234567;
	values[1] = 10;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1205.631835937\n");

	/* Zero */
	values[0] = 0;
	values[1] = 10;
	ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000000\n");
}

static void iio_test_iio_format_value_multiple(struct kunit *test)
{
	char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
	int values[] = {1, -2, 3, -4, 5};
	int ret;

	ret = iio_format_value(buf, IIO_VAL_INT_MULTIPLE,
			       ARRAY_SIZE(values), values);
	IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1 -2 3 -4 5 \n");
}

static struct kunit_case iio_format_test_cases[] = {
		KUNIT_CASE(iio_test_iio_format_value_integer),
		KUNIT_CASE(iio_test_iio_format_value_fixedpoint),
		KUNIT_CASE(iio_test_iio_format_value_fractional),
		KUNIT_CASE(iio_test_iio_format_value_fractional_log2),
		KUNIT_CASE(iio_test_iio_format_value_multiple),
		{}
};

static struct kunit_suite iio_format_test_suite = {
	.name = "iio-format",
	.test_cases = iio_format_test_cases,
};
kunit_test_suite(iio_format_test_suite);