iio-test-rescale.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kunit tests for IIO rescale conversions
  4. *
  5. * Copyright (c) 2021 Liam Beguin <[email protected]>
  6. */
  7. #include <linux/gcd.h>
  8. #include <linux/overflow.h>
  9. #include <linux/iio/afe/rescale.h>
  10. #include <linux/iio/iio.h>
  11. #include <kunit/test.h>
  12. struct rescale_tc_data {
  13. const char *name;
  14. const s32 numerator;
  15. const s32 denominator;
  16. const s32 offset;
  17. const int schan_val;
  18. const int schan_val2;
  19. const int schan_off;
  20. const int schan_scale_type;
  21. const char *expected;
  22. const char *expected_off;
  23. };
  24. static const struct rescale_tc_data scale_cases[] = {
  25. /*
  26. * Typical use cases
  27. */
  28. {
  29. .name = "typical IIO_VAL_INT, positive",
  30. .numerator = 1000000,
  31. .denominator = 8060,
  32. .schan_scale_type = IIO_VAL_INT,
  33. .schan_val = 42,
  34. .expected = "5210.918114143",
  35. },
  36. {
  37. .name = "typical IIO_VAL_INT, negative",
  38. .numerator = -1000000,
  39. .denominator = 8060,
  40. .schan_scale_type = IIO_VAL_INT,
  41. .schan_val = 42,
  42. .expected = "-5210.918114143",
  43. },
  44. {
  45. .name = "typical IIO_VAL_FRACTIONAL, positive",
  46. .numerator = 1000000,
  47. .denominator = 8060,
  48. .schan_scale_type = IIO_VAL_FRACTIONAL,
  49. .schan_val = 42,
  50. .schan_val2 = 20,
  51. .expected = "260.545905707",
  52. },
  53. {
  54. .name = "typical IIO_VAL_FRACTIONAL, negative",
  55. .numerator = -1000000,
  56. .denominator = 8060,
  57. .schan_scale_type = IIO_VAL_FRACTIONAL,
  58. .schan_val = 42,
  59. .schan_val2 = 20,
  60. .expected = "-260.545905707",
  61. },
  62. {
  63. .name = "typical IIO_VAL_FRACTIONAL_LOG2, positive",
  64. .numerator = 42,
  65. .denominator = 53,
  66. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  67. .schan_val = 4096,
  68. .schan_val2 = 16,
  69. .expected = "0.049528301",
  70. },
  71. {
  72. .name = "typical IIO_VAL_FRACTIONAL_LOG2, negative",
  73. .numerator = -42,
  74. .denominator = 53,
  75. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  76. .schan_val = 4096,
  77. .schan_val2 = 16,
  78. .expected = "-0.049528301",
  79. },
  80. {
  81. .name = "typical IIO_VAL_INT_PLUS_NANO, positive",
  82. .numerator = 1000000,
  83. .denominator = 8060,
  84. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  85. .schan_val = 10,
  86. .schan_val2 = 123456,
  87. .expected = "1240.710106203",
  88. },
  89. {
  90. .name = "typical IIO_VAL_INT_PLUS_NANO, negative",
  91. .numerator = -1000000,
  92. .denominator = 8060,
  93. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  94. .schan_val = 10,
  95. .schan_val2 = 123456,
  96. .expected = "-1240.710106203",
  97. },
  98. {
  99. .name = "typical IIO_VAL_INT_PLUS_MICRO, positive",
  100. .numerator = 1000000,
  101. .denominator = 8060,
  102. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  103. .schan_val = 10,
  104. .schan_val2 = 1234,
  105. .expected = "1240.84789",
  106. },
  107. {
  108. .name = "typical IIO_VAL_INT_PLUS_MICRO, negative",
  109. .numerator = -1000000,
  110. .denominator = 8060,
  111. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  112. .schan_val = 10,
  113. .schan_val2 = 1234,
  114. .expected = "-1240.84789",
  115. },
  116. /*
  117. * Use cases with small scales involving divisions
  118. */
  119. {
  120. .name = "small IIO_VAL_FRACTIONAL, 261/509 scaled by 90/1373754273",
  121. .numerator = 261,
  122. .denominator = 509,
  123. .schan_scale_type = IIO_VAL_FRACTIONAL,
  124. .schan_val = 90,
  125. .schan_val2 = 1373754273,
  126. .expected = "0.000000033594",
  127. },
  128. {
  129. .name = "small IIO_VAL_FRACTIONAL, 90/1373754273 scaled by 261/509",
  130. .numerator = 90,
  131. .denominator = 1373754273,
  132. .schan_scale_type = IIO_VAL_FRACTIONAL,
  133. .schan_val = 261,
  134. .schan_val2 = 509,
  135. .expected = "0.000000033594",
  136. },
  137. {
  138. .name = "small IIO_VAL_FRACTIONAL, 760/1373754273 scaled by 427/2727",
  139. .numerator = 760,
  140. .denominator = 1373754273,
  141. .schan_scale_type = IIO_VAL_FRACTIONAL,
  142. .schan_val = 427,
  143. .schan_val2 = 2727,
  144. .expected = "0.000000086626",
  145. },
  146. {
  147. .name = "small IIO_VAL_FRACTIONAL, 761/1373754273 scaled by 427/2727",
  148. .numerator = 761,
  149. .denominator = 1373754273,
  150. .schan_scale_type = IIO_VAL_FRACTIONAL,
  151. .schan_val = 427,
  152. .schan_val2 = 2727,
  153. .expected = "0.000000086740",
  154. },
  155. {
  156. .name = "small IIO_VAL_FRACTIONAL, 5/32768 scaled by 3/10000",
  157. .numerator = 5,
  158. .denominator = 32768,
  159. .schan_scale_type = IIO_VAL_FRACTIONAL,
  160. .schan_val = 3,
  161. .schan_val2 = 10000,
  162. .expected = "0.0000000457763671875",
  163. },
  164. {
  165. .name = "small IIO_VAL_FRACTIONAL, 0 < scale < 1",
  166. .numerator = 6,
  167. .denominator = 6,
  168. .schan_scale_type = IIO_VAL_FRACTIONAL,
  169. .schan_val = 1,
  170. .schan_val2 = 3,
  171. .expected = "0.3333333333333333",
  172. },
  173. {
  174. .name = "small IIO_VAL_FRACTIONAL, -1 < scale < 0",
  175. .numerator = -6,
  176. .denominator = 6,
  177. .schan_scale_type = IIO_VAL_FRACTIONAL,
  178. .schan_val = 1,
  179. .schan_val2 = 3,
  180. .expected = "-0.3333333333333333",
  181. },
  182. {
  183. .name = "small IIO_VAL_FRACTIONAL, 0 < scale < 2",
  184. .numerator = 8,
  185. .denominator = 2,
  186. .schan_scale_type = IIO_VAL_FRACTIONAL,
  187. .schan_val = 1,
  188. .schan_val2 = 3,
  189. .expected = "1.3333333333333333",
  190. },
  191. {
  192. .name = "small IIO_VAL_FRACTIONAL, -2 < scale < 0",
  193. .numerator = -8,
  194. .denominator = 2,
  195. .schan_scale_type = IIO_VAL_FRACTIONAL,
  196. .schan_val = 1,
  197. .schan_val2 = 3,
  198. .expected = "-1.3333333333333333",
  199. },
  200. {
  201. .name = "small IIO_VAL_FRACTIONAL_LOG2, 760/32768 scaled by 15/22",
  202. .numerator = 760,
  203. .denominator = 32768,
  204. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  205. .schan_val = 15,
  206. .schan_val2 = 22,
  207. .expected = "0.000000082946",
  208. },
  209. {
  210. .name = "small IIO_VAL_FRACTIONAL_LOG2, 761/32768 scaled by 15/22",
  211. .numerator = 761,
  212. .denominator = 32768,
  213. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  214. .schan_val = 15,
  215. .schan_val2 = 22,
  216. .expected = "0.000000083055",
  217. },
  218. {
  219. .name = "small IIO_VAL_FRACTIONAL_LOG2, 0 < scale < 1",
  220. .numerator = 16,
  221. .denominator = 3,
  222. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  223. .schan_val = 1,
  224. .schan_val2 = 4,
  225. .expected = "0.3333333333333333",
  226. },
  227. {
  228. .name = "small IIO_VAL_FRACTIONAL_LOG2, -1 < scale < 0",
  229. .numerator = -16,
  230. .denominator = 3,
  231. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  232. .schan_val = 1,
  233. .schan_val2 = 4,
  234. .expected = "-0.3333333333333333",
  235. },
  236. {
  237. .name = "small IIO_VAL_FRACTIONAL_LOG2, 0 < scale < 2",
  238. .numerator = 8,
  239. .denominator = 3,
  240. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  241. .schan_val = 1,
  242. .schan_val2 = 1,
  243. .expected = "1.3333333333333333",
  244. },
  245. {
  246. .name = "small IIO_VAL_FRACTIONAL_LOG2, -2 < scale < 0",
  247. .numerator = -8,
  248. .denominator = 3,
  249. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  250. .schan_val = 1,
  251. .schan_val2 = 1,
  252. .expected = "-1.3333333333333333",
  253. },
  254. {
  255. .name = "small IIO_VAL_INT_PLUS_MICRO, positive",
  256. .numerator = 1,
  257. .denominator = 2,
  258. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  259. .schan_val = 5,
  260. .schan_val2 = 1234,
  261. .expected = "2.500617",
  262. },
  263. {
  264. .name = "small IIO_VAL_INT_PLUS_MICRO, negative",
  265. .numerator = -1,
  266. .denominator = 2,
  267. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  268. .schan_val = 5,
  269. .schan_val2 = 1234,
  270. .expected = "-2.500617",
  271. },
  272. /*
  273. * INT_PLUS_{MICRO,NANO} positive/negative corner cases
  274. */
  275. {
  276. .name = "negative IIO_VAL_INT_PLUS_NANO, negative schan",
  277. .numerator = 1000000,
  278. .denominator = 8060,
  279. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  280. .schan_val = -10,
  281. .schan_val2 = 123456,
  282. .expected = "-1240.710106203",
  283. },
  284. {
  285. .name = "negative IIO_VAL_INT_PLUS_NANO, both negative",
  286. .numerator = -1000000,
  287. .denominator = 8060,
  288. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  289. .schan_val = -10,
  290. .schan_val2 = 123456,
  291. .expected = "1240.710106203",
  292. },
  293. {
  294. .name = "negative IIO_VAL_INT_PLUS_NANO, 3 negative",
  295. .numerator = -1000000,
  296. .denominator = -8060,
  297. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  298. .schan_val = -10,
  299. .schan_val2 = 123456,
  300. .expected = "-1240.710106203",
  301. },
  302. {
  303. .name = "negative IIO_VAL_INT_PLUS_NANO, 4 negative",
  304. .numerator = -1000000,
  305. .denominator = -8060,
  306. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  307. .schan_val = -10,
  308. .schan_val2 = -123456,
  309. .expected = "-1240.710106203",
  310. },
  311. {
  312. .name = "negative IIO_VAL_INT_PLUS_NANO, negative, *val = 0",
  313. .numerator = 1,
  314. .denominator = -10,
  315. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  316. .schan_val = 0,
  317. .schan_val2 = 123456789,
  318. .expected = "-0.012345678",
  319. },
  320. /*
  321. * INT_PLUS_{MICRO,NANO} decimal part overflow
  322. */
  323. {
  324. .name = "decimal overflow IIO_VAL_INT_PLUS_NANO, positive",
  325. .numerator = 1000000,
  326. .denominator = 8060,
  327. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  328. .schan_val = 10,
  329. .schan_val2 = 123456789,
  330. .expected = "1256.01200856",
  331. },
  332. {
  333. .name = "decimal overflow IIO_VAL_INT_PLUS_NANO, negative",
  334. .numerator = -1000000,
  335. .denominator = 8060,
  336. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  337. .schan_val = 10,
  338. .schan_val2 = 123456789,
  339. .expected = "-1256.01200856",
  340. },
  341. {
  342. .name = "decimal overflow IIO_VAL_INT_PLUS_NANO, negative schan",
  343. .numerator = 1000000,
  344. .denominator = 8060,
  345. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  346. .schan_val = -10,
  347. .schan_val2 = 123456789,
  348. .expected = "-1256.01200856",
  349. },
  350. {
  351. .name = "decimal overflow IIO_VAL_INT_PLUS_MICRO, positive",
  352. .numerator = 1000000,
  353. .denominator = 8060,
  354. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  355. .schan_val = 10,
  356. .schan_val2 = 123456789,
  357. .expected = "16557.914267",
  358. },
  359. {
  360. .name = "decimal overflow IIO_VAL_INT_PLUS_MICRO, negative",
  361. .numerator = -1000000,
  362. .denominator = 8060,
  363. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  364. .schan_val = 10,
  365. .schan_val2 = 123456789,
  366. .expected = "-16557.914267",
  367. },
  368. {
  369. .name = "decimal overflow IIO_VAL_INT_PLUS_MICRO, negative schan",
  370. .numerator = 1000000,
  371. .denominator = 8060,
  372. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  373. .schan_val = -10,
  374. .schan_val2 = 123456789,
  375. .expected = "-16557.914267",
  376. },
  377. /*
  378. * 32-bit overflow conditions
  379. */
  380. {
  381. .name = "overflow IIO_VAL_FRACTIONAL, positive",
  382. .numerator = 2,
  383. .denominator = 20,
  384. .schan_scale_type = IIO_VAL_FRACTIONAL,
  385. .schan_val = S32_MAX,
  386. .schan_val2 = 1,
  387. .expected = "214748364.7",
  388. },
  389. {
  390. .name = "overflow IIO_VAL_FRACTIONAL, negative",
  391. .numerator = -2,
  392. .denominator = 20,
  393. .schan_scale_type = IIO_VAL_FRACTIONAL,
  394. .schan_val = S32_MAX,
  395. .schan_val2 = 1,
  396. .expected = "-214748364.7",
  397. },
  398. {
  399. .name = "overflow IIO_VAL_FRACTIONAL_LOG2, positive",
  400. .numerator = S32_MAX,
  401. .denominator = 4096,
  402. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  403. .schan_val = 4096,
  404. .schan_val2 = 16,
  405. .expected = "32767.99998474121",
  406. },
  407. {
  408. .name = "overflow IIO_VAL_FRACTIONAL_LOG2, negative",
  409. .numerator = S32_MAX,
  410. .denominator = 4096,
  411. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  412. .schan_val = -4096,
  413. .schan_val2 = 16,
  414. .expected = "-32767.99998474121",
  415. },
  416. {
  417. .name = "overflow IIO_VAL_INT_PLUS_NANO, positive",
  418. .numerator = 2,
  419. .denominator = 20,
  420. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  421. .schan_val = 10,
  422. .schan_val2 = S32_MAX,
  423. .expected = "1.214748364",
  424. },
  425. {
  426. .name = "overflow IIO_VAL_INT_PLUS_NANO, negative",
  427. .numerator = -2,
  428. .denominator = 20,
  429. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  430. .schan_val = 10,
  431. .schan_val2 = S32_MAX,
  432. .expected = "-1.214748364",
  433. },
  434. {
  435. .name = "overflow IIO_VAL_INT_PLUS_NANO, negative schan",
  436. .numerator = 2,
  437. .denominator = 20,
  438. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  439. .schan_val = -10,
  440. .schan_val2 = S32_MAX,
  441. .expected = "-1.214748364",
  442. },
  443. {
  444. .name = "overflow IIO_VAL_INT_PLUS_MICRO, positive",
  445. .numerator = 2,
  446. .denominator = 20,
  447. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  448. .schan_val = 10,
  449. .schan_val2 = S32_MAX,
  450. .expected = "215.748364",
  451. },
  452. {
  453. .name = "overflow IIO_VAL_INT_PLUS_MICRO, negative",
  454. .numerator = -2,
  455. .denominator = 20,
  456. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  457. .schan_val = 10,
  458. .schan_val2 = S32_MAX,
  459. .expected = "-215.748364",
  460. },
  461. {
  462. .name = "overflow IIO_VAL_INT_PLUS_MICRO, negative schan",
  463. .numerator = 2,
  464. .denominator = 20,
  465. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  466. .schan_val = -10,
  467. .schan_val2 = S32_MAX,
  468. .expected = "-215.748364",
  469. },
  470. };
  471. static const struct rescale_tc_data offset_cases[] = {
  472. /*
  473. * Typical use cases
  474. */
  475. {
  476. .name = "typical IIO_VAL_INT, positive",
  477. .offset = 1234,
  478. .schan_scale_type = IIO_VAL_INT,
  479. .schan_val = 123,
  480. .schan_val2 = 0,
  481. .schan_off = 14,
  482. .expected_off = "24", /* 23.872 */
  483. },
  484. {
  485. .name = "typical IIO_VAL_INT, negative",
  486. .offset = -1234,
  487. .schan_scale_type = IIO_VAL_INT,
  488. .schan_val = 12,
  489. .schan_val2 = 0,
  490. .schan_off = 14,
  491. .expected_off = "-88", /* -88.83333333333333 */
  492. },
  493. {
  494. .name = "typical IIO_VAL_FRACTIONAL, positive",
  495. .offset = 1234,
  496. .schan_scale_type = IIO_VAL_FRACTIONAL,
  497. .schan_val = 12,
  498. .schan_val2 = 34,
  499. .schan_off = 14,
  500. .expected_off = "3510", /* 3510.333333333333 */
  501. },
  502. {
  503. .name = "typical IIO_VAL_FRACTIONAL, negative",
  504. .offset = -1234,
  505. .schan_scale_type = IIO_VAL_FRACTIONAL,
  506. .schan_val = 12,
  507. .schan_val2 = 34,
  508. .schan_off = 14,
  509. .expected_off = "-3482", /* -3482.333333333333 */
  510. },
  511. {
  512. .name = "typical IIO_VAL_FRACTIONAL_LOG2, positive",
  513. .offset = 1234,
  514. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  515. .schan_val = 12,
  516. .schan_val2 = 16,
  517. .schan_off = 14,
  518. .expected_off = "6739299", /* 6739299.333333333 */
  519. },
  520. {
  521. .name = "typical IIO_VAL_FRACTIONAL_LOG2, negative",
  522. .offset = -1234,
  523. .schan_scale_type = IIO_VAL_FRACTIONAL_LOG2,
  524. .schan_val = 12,
  525. .schan_val2 = 16,
  526. .schan_off = 14,
  527. .expected_off = "-6739271", /* -6739271.333333333 */
  528. },
  529. {
  530. .name = "typical IIO_VAL_INT_PLUS_NANO, positive",
  531. .offset = 1234,
  532. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  533. .schan_val = 10,
  534. .schan_val2 = 123456789,
  535. .schan_off = 14,
  536. .expected_off = "135", /* 135.8951219647469 */
  537. },
  538. {
  539. .name = "typical IIO_VAL_INT_PLUS_NANO, negative",
  540. .offset = -1234,
  541. .schan_scale_type = IIO_VAL_INT_PLUS_NANO,
  542. .schan_val = 10,
  543. .schan_val2 = 123456789,
  544. .schan_off = 14,
  545. .expected_off = "-107", /* -107.89512196474689 */
  546. },
  547. {
  548. .name = "typical IIO_VAL_INT_PLUS_MICRO, positive",
  549. .offset = 1234,
  550. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  551. .schan_val = 10,
  552. .schan_val2 = 123456789,
  553. .schan_off = 14,
  554. .expected_off = "23", /* 23.246438560723952 */
  555. },
  556. {
  557. .name = "typical IIO_VAL_INT_PLUS_MICRO, negative",
  558. .offset = -12345,
  559. .schan_scale_type = IIO_VAL_INT_PLUS_MICRO,
  560. .schan_val = 10,
  561. .schan_val2 = 123456789,
  562. .schan_off = 14,
  563. .expected_off = "-78", /* -78.50185091745313 */
  564. },
  565. };
  566. static void case_to_desc(const struct rescale_tc_data *t, char *desc)
  567. {
  568. strcpy(desc, t->name);
  569. }
  570. KUNIT_ARRAY_PARAM(iio_rescale_scale, scale_cases, case_to_desc);
  571. KUNIT_ARRAY_PARAM(iio_rescale_offset, offset_cases, case_to_desc);
  572. /**
  573. * iio_str_to_nano() - Parse a fixed-point string to get an
  574. * IIO_VAL_INT_PLUS_NANO value
  575. * @str: The string to parse
  576. * @nano: The number as an integer
  577. *
  578. * Returns 0 on success, or a negative error code if the string cound not be
  579. * parsed.
  580. */
  581. static int iio_str_to_nano(const char *str, s64 *nano)
  582. {
  583. int tmp, tmp2;
  584. int ret = 0;
  585. /*
  586. * iio_str_to_fixpoint() uses 10^8 here instead of 10^9 as fract_mult is
  587. * the multiplier for the first decimal place.
  588. */
  589. ret = iio_str_to_fixpoint(str, 100000000, &tmp, &tmp2);
  590. if (ret < 0)
  591. return ret;
  592. if (tmp < 0)
  593. tmp2 *= -1;
  594. *nano = (s64)tmp * 1000000000UL + tmp2;
  595. return ret;
  596. }
  597. /**
  598. * iio_test_relative_error_ppm() - Compute relative error (in parts-per-million)
  599. * between two fixed-point strings
  600. * @real_str: The real value as a string
  601. * @exp_str: The expected value as a string
  602. *
  603. * Returns a negative error code if the strings cound not be parsed, or the
  604. * relative error in parts-per-million.
  605. */
  606. static int iio_test_relative_error_ppm(const char *real_str, const char *exp_str)
  607. {
  608. s64 real, exp, err;
  609. int ret;
  610. ret = iio_str_to_nano(real_str, &real);
  611. if (ret < 0)
  612. return ret;
  613. ret = iio_str_to_nano(exp_str, &exp);
  614. if (ret < 0)
  615. return ret;
  616. if (!exp) {
  617. pr_err("Expected value is null, relative error is undefined\n");
  618. return -EINVAL;
  619. }
  620. err = 1000000UL * abs(exp - real);
  621. return (int)div64_u64(err, abs(exp));
  622. }
  623. static void iio_rescale_test_scale(struct kunit *test)
  624. {
  625. struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value;
  626. char *buff = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
  627. struct rescale rescale;
  628. int values[2];
  629. int rel_ppm;
  630. int ret;
  631. rescale.numerator = t->numerator;
  632. rescale.denominator = t->denominator;
  633. rescale.offset = t->offset;
  634. values[0] = t->schan_val;
  635. values[1] = t->schan_val2;
  636. ret = rescale_process_scale(&rescale, t->schan_scale_type,
  637. &values[0], &values[1]);
  638. ret = iio_format_value(buff, ret, 2, values);
  639. KUNIT_EXPECT_EQ(test, (int)strlen(buff), ret);
  640. rel_ppm = iio_test_relative_error_ppm(buff, t->expected);
  641. KUNIT_EXPECT_GE_MSG(test, rel_ppm, 0, "failed to compute ppm\n");
  642. KUNIT_EXPECT_EQ_MSG(test, rel_ppm, 0,
  643. "\t real=%s"
  644. "\texpected=%s\n",
  645. buff, t->expected);
  646. }
  647. static void iio_rescale_test_offset(struct kunit *test)
  648. {
  649. struct rescale_tc_data *t = (struct rescale_tc_data *)test->param_value;
  650. char *buff_off = kunit_kmalloc(test, PAGE_SIZE, GFP_KERNEL);
  651. struct rescale rescale;
  652. int values[2];
  653. int ret;
  654. rescale.numerator = t->numerator;
  655. rescale.denominator = t->denominator;
  656. rescale.offset = t->offset;
  657. values[0] = t->schan_val;
  658. values[1] = t->schan_val2;
  659. ret = rescale_process_offset(&rescale, t->schan_scale_type,
  660. t->schan_val, t->schan_val2, t->schan_off,
  661. &values[0], &values[1]);
  662. ret = iio_format_value(buff_off, ret, 2, values);
  663. KUNIT_EXPECT_EQ(test, (int)strlen(buff_off), ret);
  664. KUNIT_EXPECT_STREQ(test, strim(buff_off), t->expected_off);
  665. }
  666. static struct kunit_case iio_rescale_test_cases[] = {
  667. KUNIT_CASE_PARAM(iio_rescale_test_scale, iio_rescale_scale_gen_params),
  668. KUNIT_CASE_PARAM(iio_rescale_test_offset, iio_rescale_offset_gen_params),
  669. {}
  670. };
  671. static struct kunit_suite iio_rescale_test_suite = {
  672. .name = "iio-rescale",
  673. .test_cases = iio_rescale_test_cases,
  674. };
  675. kunit_test_suite(iio_rescale_test_suite);
  676. MODULE_AUTHOR("Liam Beguin <[email protected]>");
  677. MODULE_DESCRIPTION("Test IIO rescale conversion functions");
  678. MODULE_LICENSE("GPL v2");
  679. MODULE_IMPORT_NS(IIO_RESCALE);