analog.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2015 Uwe Hermann <uwe@hermann-uwe.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <check.h>
  23. #include <libsigrok/libsigrok.h>
  24. #include "lib.h"
  25. static int sr_analog_init_(struct sr_datafeed_analog *analog,
  26. struct sr_analog_encoding *encoding,
  27. struct sr_analog_meaning *meaning,
  28. struct sr_analog_spec *spec,
  29. int digits)
  30. {
  31. memset(analog, 0, sizeof(*analog));
  32. memset(encoding, 0, sizeof(*encoding));
  33. memset(meaning, 0, sizeof(*meaning));
  34. memset(spec, 0, sizeof(*spec));
  35. analog->encoding = encoding;
  36. analog->meaning = meaning;
  37. analog->spec = spec;
  38. encoding->unitsize = sizeof(float);
  39. encoding->is_float = TRUE;
  40. #ifdef WORDS_BIGENDIAN
  41. encoding->is_bigendian = TRUE;
  42. #else
  43. encoding->is_bigendian = FALSE;
  44. #endif
  45. encoding->digits = digits;
  46. encoding->is_digits_decimal = TRUE;
  47. encoding->scale.p = 1;
  48. encoding->scale.q = 1;
  49. encoding->offset.p = 0;
  50. encoding->offset.q = 1;
  51. spec->spec_digits = digits;
  52. return SR_OK;
  53. }
  54. START_TEST(test_analog_to_float)
  55. {
  56. int ret;
  57. unsigned int i;
  58. float f, fout;
  59. struct sr_channel ch;
  60. struct sr_datafeed_analog analog;
  61. struct sr_analog_encoding encoding;
  62. struct sr_analog_meaning meaning;
  63. struct sr_analog_spec spec;
  64. const float v[] = {-12.9, -333.999, 0, 3.1415, 29.7, 989898.121212};
  65. sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
  66. analog.num_samples = 1;
  67. analog.data = &f;
  68. meaning.channels = g_slist_append(NULL, &ch);
  69. for (i = 0; i < ARRAY_SIZE(v); i++) {
  70. fout = 19;
  71. f = v[i];
  72. ret = sr_analog_to_float(&analog, &fout);
  73. fail_unless(ret == SR_OK, "sr_analog_to_float() failed: %d.", ret);
  74. fail_unless(fabs(f - fout) <= 0.001, "%f != %f", f, fout);
  75. }
  76. }
  77. END_TEST
  78. START_TEST(test_analog_to_float_null)
  79. {
  80. int ret;
  81. float f, fout;
  82. struct sr_datafeed_analog analog;
  83. struct sr_analog_encoding encoding;
  84. struct sr_analog_meaning meaning;
  85. struct sr_analog_spec spec;
  86. f = G_PI;
  87. sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
  88. analog.num_samples = 1;
  89. analog.data = &f;
  90. ret = sr_analog_to_float(NULL, &fout);
  91. fail_unless(ret == SR_ERR_ARG);
  92. ret = sr_analog_to_float(&analog, NULL);
  93. fail_unless(ret == SR_ERR_ARG);
  94. ret = sr_analog_to_float(NULL, NULL);
  95. fail_unless(ret == SR_ERR_ARG);
  96. analog.data = NULL;
  97. ret = sr_analog_to_float(&analog, &fout);
  98. fail_unless(ret == SR_ERR_ARG);
  99. analog.data = &f;
  100. analog.meaning = NULL;
  101. ret = sr_analog_to_float(&analog, &fout);
  102. fail_unless(ret == SR_ERR_ARG);
  103. analog.meaning = &meaning;
  104. analog.encoding = NULL;
  105. ret = sr_analog_to_float(&analog, &fout);
  106. fail_unless(ret == SR_ERR_ARG);
  107. analog.encoding = &encoding;
  108. }
  109. END_TEST
  110. START_TEST(test_analog_si_prefix)
  111. {
  112. struct {
  113. float input_value;
  114. int input_digits;
  115. float output_value;
  116. int output_digits;
  117. const char *output_si_prefix;
  118. } v[] = {
  119. { 12.0 , 0, 12.0 , 0, "" },
  120. { 12.0 , 1, 12.0 , 1, "" },
  121. { 12.0 , -1, 0.012, 2, "k" },
  122. { 1024.0 , 0, 1.024, 3, "k" },
  123. { 1024.0 , -1, 1.024, 2, "k" },
  124. { 1024.0 , -3, 1.024, 0, "k" },
  125. { 12.0e5 , 0, 1.2, 6, "M" },
  126. { 0.123456, 0, 0.123456, 0, "" },
  127. { 0.123456, 1, 0.123456, 1, "" },
  128. { 0.123456, 2, 0.123456, 2, "" },
  129. { 0.123456, 3, 123.456, 0, "m" },
  130. { 0.123456, 4, 123.456, 1, "m" },
  131. { 0.123456, 5, 123.456, 2, "m" },
  132. { 0.123456, 6, 123.456, 3, "m" },
  133. { 0.123456, 7, 123.456, 4, "m" },
  134. { 0.0123 , 4, 12.3, 1, "m" },
  135. { 0.00123 , 5, 1.23, 2, "m" },
  136. { 0.000123, 4, 0.123, 1, "m" },
  137. { 0.000123, 5, 0.123, 2, "m" },
  138. { 0.000123, 6, 123.0, 0, "µ" },
  139. { 0.000123, 7, 123.0, 1, "µ" },
  140. };
  141. for (unsigned int i = 0; i < ARRAY_SIZE(v); i++) {
  142. float value = v[i].input_value;
  143. int digits = v[i].input_digits;
  144. const char *si_prefix = sr_analog_si_prefix(&value, &digits);
  145. fail_unless(fabs(value - v[i].output_value) <= 0.00001,
  146. "sr_analog_si_prefix() unexpected output value %f (i=%d).",
  147. value , i);
  148. fail_unless(digits == v[i].output_digits,
  149. "sr_analog_si_prefix() unexpected output digits %d (i=%d).",
  150. digits, i);
  151. fail_unless(!strcmp(si_prefix, v[i].output_si_prefix),
  152. "sr_analog_si_prefix() unexpected output prefix \"%s\" (i=%d).",
  153. si_prefix, i);
  154. }
  155. }
  156. END_TEST
  157. START_TEST(test_analog_si_prefix_null)
  158. {
  159. float value = 1.23;
  160. int digits = 1;
  161. const char *si_prefix;
  162. si_prefix = sr_analog_si_prefix(NULL, &digits);
  163. fail_unless(!strcmp(si_prefix, ""));
  164. si_prefix = sr_analog_si_prefix(&value, NULL);
  165. fail_unless(!strcmp(si_prefix, ""));
  166. si_prefix = sr_analog_si_prefix(NULL, NULL);
  167. fail_unless(!strcmp(si_prefix, ""));
  168. }
  169. END_TEST
  170. START_TEST(test_analog_unit_to_string)
  171. {
  172. int ret;
  173. unsigned int i;
  174. char *result;
  175. struct sr_datafeed_analog analog;
  176. struct sr_analog_encoding encoding;
  177. struct sr_analog_meaning meaning;
  178. struct sr_analog_spec spec;
  179. const char *r[] = {" V RMS"};
  180. sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
  181. for (i = -1; i < ARRAY_SIZE(r); i++) {
  182. meaning.unit = SR_UNIT_VOLT;
  183. meaning.mqflags = SR_MQFLAG_RMS;
  184. ret = sr_analog_unit_to_string(&analog, &result);
  185. fail_unless(ret == SR_OK);
  186. fail_unless(result != NULL);
  187. fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
  188. g_free(result);
  189. }
  190. }
  191. END_TEST
  192. START_TEST(test_analog_unit_to_string_null)
  193. {
  194. int ret;
  195. char *result;
  196. struct sr_datafeed_analog analog;
  197. struct sr_analog_encoding encoding;
  198. struct sr_analog_meaning meaning;
  199. struct sr_analog_spec spec;
  200. sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
  201. meaning.unit = SR_UNIT_VOLT;
  202. meaning.mqflags = SR_MQFLAG_RMS;
  203. ret = sr_analog_unit_to_string(NULL, &result);
  204. fail_unless(ret == SR_ERR_ARG);
  205. ret = sr_analog_unit_to_string(&analog, NULL);
  206. fail_unless(ret == SR_ERR_ARG);
  207. ret = sr_analog_unit_to_string(NULL, NULL);
  208. fail_unless(ret == SR_ERR_ARG);
  209. analog.meaning = NULL;
  210. ret = sr_analog_unit_to_string(&analog, &result);
  211. fail_unless(ret == SR_ERR_ARG);
  212. }
  213. END_TEST
  214. START_TEST(test_set_rational)
  215. {
  216. unsigned int i;
  217. struct sr_rational r;
  218. const int64_t p[] = {0, 1, -5, INT64_MAX};
  219. const uint64_t q[] = {0, 2, 7, UINT64_MAX};
  220. for (i = 0; i < ARRAY_SIZE(p); i++) {
  221. sr_rational_set(&r, p[i], q[i]);
  222. fail_unless(r.p == p[i] && r.q == q[i]);
  223. }
  224. }
  225. END_TEST
  226. START_TEST(test_set_rational_null)
  227. {
  228. sr_rational_set(NULL, 5, 7);
  229. }
  230. END_TEST
  231. START_TEST(test_cmp_rational)
  232. {
  233. const struct sr_rational r[] = { { 1, 1 },
  234. { 2, 2 },
  235. { 1000, 1000 },
  236. { INT64_MAX, INT64_MAX },
  237. { 1, 4 },
  238. { 2, 8 },
  239. { INT64_MAX, UINT64_MAX },
  240. { INT64_MIN, UINT64_MAX },
  241. };
  242. fail_unless(sr_rational_eq(&r[0], &r[0]) == 1);
  243. fail_unless(sr_rational_eq(&r[0], &r[1]) == 1);
  244. fail_unless(sr_rational_eq(&r[1], &r[2]) == 1);
  245. fail_unless(sr_rational_eq(&r[2], &r[3]) == 1);
  246. fail_unless(sr_rational_eq(&r[3], &r[3]) == 1);
  247. fail_unless(sr_rational_eq(&r[4], &r[4]) == 1);
  248. fail_unless(sr_rational_eq(&r[4], &r[5]) == 1);
  249. fail_unless(sr_rational_eq(&r[5], &r[5]) == 1);
  250. fail_unless(sr_rational_eq(&r[6], &r[6]) == 1);
  251. fail_unless(sr_rational_eq(&r[7], &r[7]) == 1);
  252. fail_unless(sr_rational_eq(&r[1], &r[4]) == 0);
  253. }
  254. END_TEST
  255. START_TEST(test_mult_rational)
  256. {
  257. const struct sr_rational r[][3] = {
  258. /* a * b = c */
  259. { { 1, 1 }, { 1, 1 }, { 1, 1 }},
  260. { { 2, 1 }, { 3, 1 }, { 6, 1 }},
  261. { { 1, 2 }, { 2, 1 }, { 1, 1 }},
  262. /* Test negative numbers */
  263. { { -1, 2 }, { 2, 1 }, { -1, 1 }},
  264. { { -1, 2 }, { -2, 1 }, { 1, 1 }},
  265. { { -(1ll<<20), (1ll<<10) }, { -(1ll<<20), 1 }, { (1ll<<30), 1 }},
  266. /* Test reduction */
  267. { { INT32_MAX, (1ll<<12) }, { (1<<2), 1 }, { INT32_MAX, (1ll<<10) }},
  268. { { INT64_MAX, (1ll<<63) }, { (1<<3), 1 }, { INT64_MAX, (1ll<<60) }},
  269. /* Test large numbers */
  270. { { (1ll<<40), (1ll<<10) }, { (1ll<<30), 1 }, { (1ll<<60), 1 }},
  271. { { -(1ll<<40), (1ll<<10) }, { -(1ll<<30), 1 }, { (1ll<<60), 1 }},
  272. { { 1000, 1 }, { 8000, 1 }, { 8000000, 1 }},
  273. { { 10000, 1 }, { 80000, 1 }, { 800000000, 1 }},
  274. { { 10000*3, 4 }, { 80000*3, 1 }, { 200000000*9, 1 }},
  275. { { 1, 1000 }, { 1, 8000 }, { 1, 8000000 }},
  276. { { 1, 10000 }, { 1, 80000 }, { 1, 800000000 }},
  277. { { 4, 10000*3 }, { 1, 80000*3 }, { 1, 200000000*9 }},
  278. { { -10000*3, 4 }, { 80000*3, 1 }, { -200000000*9, 1 }},
  279. { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }},
  280. };
  281. for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
  282. struct sr_rational res;
  283. int rc = sr_rational_mult(&res, &r[i][0], &r[i][1]);
  284. fail_unless(rc == SR_OK);
  285. fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
  286. "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
  287. i, res.p, res.q, r[i][2].p, r[i][2].q);
  288. }
  289. }
  290. END_TEST
  291. START_TEST(test_div_rational)
  292. {
  293. const struct sr_rational r[][3] = {
  294. /* a * b = c */
  295. { { 1, 1 }, { 1, 1 }, { 1, 1 }},
  296. { { 2, 1 }, { 1, 3 }, { 6, 1 }},
  297. { { 1, 2 }, { 1, 2 }, { 1, 1 }},
  298. /* Test negative numbers */
  299. { { -1, 2 }, { 1, 2 }, { -1, 1 }},
  300. { { -1, 2 }, { -1, 2 }, { 1, 1 }},
  301. { { -(1ll<<20), (1ll<<10) }, { -1, (1ll<<20) }, { (1ll<<30), 1 }},
  302. /* Test reduction */
  303. { { INT32_MAX, (1ll<<12) }, { 1, (1<<2) }, { INT32_MAX, (1ll<<10) }},
  304. { { INT64_MAX, (1ll<<63) }, { 1, (1<<3) }, { INT64_MAX, (1ll<<60) }},
  305. /* Test large numbers */
  306. { { (1ll<<40), (1ll<<10) }, { 1, (1ll<<30) }, { (1ll<<60), 1 }},
  307. { { -(1ll<<40), (1ll<<10) }, { -1, (1ll<<30) }, { (1ll<<60), 1 }},
  308. { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
  309. { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
  310. { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
  311. { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
  312. };
  313. for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
  314. struct sr_rational res;
  315. int rc = sr_rational_div(&res, &r[i][0], &r[i][1]);
  316. fail_unless(rc == SR_OK);
  317. fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
  318. "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
  319. i, res.p, res.q, r[i][2].p, r[i][2].q);
  320. }
  321. {
  322. struct sr_rational res;
  323. int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
  324. fail_unless(rc == SR_ERR_ARG);
  325. }
  326. }
  327. END_TEST
  328. Suite *suite_analog(void)
  329. {
  330. Suite *s;
  331. TCase *tc;
  332. s = suite_create("analog");
  333. tc = tcase_create("analog_to_float");
  334. tcase_add_test(tc, test_analog_to_float);
  335. tcase_add_test(tc, test_analog_to_float_null);
  336. tcase_add_test(tc, test_analog_si_prefix);
  337. tcase_add_test(tc, test_analog_si_prefix_null);
  338. tcase_add_test(tc, test_analog_unit_to_string);
  339. tcase_add_test(tc, test_analog_unit_to_string_null);
  340. tcase_add_test(tc, test_set_rational);
  341. tcase_add_test(tc, test_set_rational_null);
  342. tcase_add_test(tc, test_cmp_rational);
  343. tcase_add_test(tc, test_mult_rational);
  344. tcase_add_test(tc, test_div_rational);
  345. suite_add_tcase(s, tc);
  346. return s;
  347. }