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 = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); 18 int val; 19 int ret; 20 21 val = 42; 22 ret = iio_format_value(buf, IIO_VAL_INT, 1, &val); 23 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "42\n"); 24 25 val = -23; 26 ret = iio_format_value(buf, IIO_VAL_INT, 1, &val); 27 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-23\n"); 28 29 val = 0; 30 ret = iio_format_value(buf, IIO_VAL_INT, 1, &val); 31 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0\n"); 32 33 val = INT_MAX; 34 ret = iio_format_value(buf, IIO_VAL_INT, 1, &val); 35 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "2147483647\n"); 36 37 val = INT_MIN; 38 ret = iio_format_value(buf, IIO_VAL_INT, 1, &val); 39 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-2147483648\n"); 40 } 41 42 static void iio_test_iio_format_value_fixedpoint(struct kunit *test) 43 { 44 char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); 45 int values[2]; 46 int ret; 47 48 /* positive >= 1 */ 49 values[0] = 1; 50 values[1] = 10; 51 52 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values); 53 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000010\n"); 54 55 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values); 56 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000010 dB\n"); 57 58 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values); 59 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1.000000010\n"); 60 61 /* positive < 1 */ 62 values[0] = 0; 63 values[1] = 12; 64 65 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values); 66 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000012\n"); 67 68 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values); 69 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000012 dB\n"); 70 71 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values); 72 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000012\n"); 73 74 /* negative <= -1 */ 75 values[0] = -1; 76 values[1] = 10; 77 78 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values); 79 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000010\n"); 80 81 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values); 82 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000010 dB\n"); 83 84 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values); 85 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1.000000010\n"); 86 87 /* negative > -1 */ 88 values[0] = 0; 89 values[1] = -123; 90 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, values); 91 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000123\n"); 92 93 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO_DB, 2, values); 94 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000123 dB\n"); 95 96 ret = iio_format_value(buf, IIO_VAL_INT_PLUS_NANO, 2, values); 97 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000000123\n"); 98 } 99 100 static void iio_test_iio_format_value_fractional(struct kunit *test) 101 { 102 char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); 103 int values[2]; 104 int ret; 105 106 /* positive < 1 */ 107 values[0] = 1; 108 values[1] = 10; 109 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values); 110 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.100000000\n"); 111 112 /* positive >= 1 */ 113 values[0] = 100; 114 values[1] = 3; 115 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values); 116 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "33.333333333\n"); 117 118 /* negative > -1 */ 119 values[0] = -1; 120 values[1] = 1000000000; 121 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values); 122 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.000000001\n"); 123 124 /* negative <= -1 */ 125 values[0] = -200; 126 values[1] = 3; 127 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values); 128 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-66.666666666\n"); 129 130 /* Zero */ 131 values[0] = 0; 132 values[1] = -10; 133 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, values); 134 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000000\n"); 135 } 136 137 static void iio_test_iio_format_value_fractional_log2(struct kunit *test) 138 { 139 char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); 140 int values[2]; 141 int ret; 142 143 /* positive < 1 */ 144 values[0] = 123; 145 values[1] = 10; 146 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values); 147 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.120117187\n"); 148 149 /* positive >= 1 */ 150 values[0] = 1234567; 151 values[1] = 10; 152 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values); 153 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1205.631835937\n"); 154 155 /* negative > -1 */ 156 values[0] = -123; 157 values[1] = 10; 158 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values); 159 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-0.120117187\n"); 160 161 /* negative <= -1 */ 162 values[0] = -1234567; 163 values[1] = 10; 164 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values); 165 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "-1205.631835937\n"); 166 167 /* Zero */ 168 values[0] = 0; 169 values[1] = 10; 170 ret = iio_format_value(buf, IIO_VAL_FRACTIONAL_LOG2, 2, values); 171 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "0.000000000\n"); 172 } 173 174 static void iio_test_iio_format_value_multiple(struct kunit *test) 175 { 176 char *buf = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL); 177 int values[] = {1, -2, 3, -4, 5}; 178 int ret; 179 180 ret = iio_format_value(buf, IIO_VAL_INT_MULTIPLE, 181 ARRAY_SIZE(values), values); 182 IIO_TEST_FORMAT_EXPECT_EQ(test, buf, ret, "1 -2 3 -4 5 \n"); 183 } 184 185 static struct kunit_case iio_format_test_cases[] = { 186 KUNIT_CASE(iio_test_iio_format_value_integer), 187 KUNIT_CASE(iio_test_iio_format_value_fixedpoint), 188 KUNIT_CASE(iio_test_iio_format_value_fractional), 189 KUNIT_CASE(iio_test_iio_format_value_fractional_log2), 190 KUNIT_CASE(iio_test_iio_format_value_multiple), 191 {} 192 }; 193 194 static struct kunit_suite iio_format_test_suite = { 195 .name = "iio-format", 196 .test_cases = iio_format_test_cases, 197 }; 198 kunit_test_suite(iio_format_test_suite); 199