tests.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  9. #pragma GCC diagnostic ignored "-Wtype-limits"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include <check.h>
  14. #include "minmea.h"
  15. static const char *valid_sentences_nochecksum[] = {
  16. "$GPTXT,xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  17. };
  18. static const char *valid_sentences_checksum[] = {
  19. "$GPTXT,01,01,02,ANTSTATUS=INIT*25",
  20. "$GPRMC,,V,,,,,,,,,,N*53",
  21. "$GPVTG,,,,,,,,,N*30",
  22. "$GPGGA,,,,,,0,00,99.99,,,,,,*48",
  23. "$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30",
  24. "$GPGLL,,,,,,V,N*64",
  25. "$GPXTE,A,A,0.67,L,N*6F",
  26. "$GPXTE,A,A,0.67,L,N*6f",
  27. "$GPGGA,123204.00,5106.94086,N,01701.51680,E,1,06,3.86,127.9,M,40.5,M,,*51",
  28. "$GPGSA,A,3,02,08,09,05,04,26,,,,,,,4.92,3.86,3.05*00",
  29. "$GPGSV,4,1,13,02,28,259,33,04,12,212,27,05,34,305,30,07,79,138,*7F",
  30. "$GPGSV,4,2,13,08,51,203,30,09,45,215,28,10,69,197,19,13,47,081,*76",
  31. "$GPGSV,4,3,13,16,20,040,17,26,08,271,30,28,01,168,18,33,24,219,27*74",
  32. "$GPGSV,4,4,13,39,31,170,27*40",
  33. "$GPGLL,5106.94086,N,01701.51680,E,123204.00,A,A*63",
  34. "$GPRMC,123205.00,A,5106.94085,N,01701.51689,E,0.016,,280214,,,A*7B",
  35. "$GPVTG,,T,,M,0.016,N,0.030,K,A*27",
  36. "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58",
  37. NULL,
  38. };
  39. static const char *invalid_sentences[] = {
  40. "$GPTXT,xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  41. "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
  42. "$GPTXT,01,01,02,ANTSTATUS=INIT*26",
  43. "$GPRMC,,V,,,,,,,,,,N*532",
  44. "$GPVTG,,,,\xff,,,,,N*30",
  45. "$$GPGGA,,,,,,0,00,99.99,,,,,,*48",
  46. "GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30",
  47. "gps: $GPGLL,,,,,,V,N",
  48. "$GPXTE,A,A,0.67,L,N*6e",
  49. "$GPXTE,A,A,0.67,L,N*6g",
  50. NULL,
  51. };
  52. START_TEST(test_minmea_checksum)
  53. {
  54. ck_assert_int_eq(minmea_checksum(""), 0x00);
  55. ck_assert_int_eq(minmea_checksum("$"), 0x00);
  56. ck_assert_int_eq(minmea_checksum("*"), 0x00);
  57. ck_assert_int_eq(minmea_checksum("$*"), 0x00);
  58. ck_assert_int_eq(minmea_checksum("$GPTXT,01,01,02,ANTSTATUS=INIT*25"), 0x25);
  59. ck_assert_int_eq(minmea_checksum("$GPTXT,01,01,02,ANTSTATUS=INIT"), 0x25);
  60. ck_assert_int_eq(minmea_checksum("GPTXT,01,01,02,ANTSTATUS=INIT*25"), 0x25);
  61. ck_assert_int_eq(minmea_checksum("GPTXT,01,01,02,ANTSTATUS=INIT"), 0x25);
  62. ck_assert_int_eq(minmea_checksum("$GPXTE,A,A,0.67,L,N*6F"), 0x6f);
  63. ck_assert_int_eq(minmea_checksum("GPXTE,A,A,0.67,L,N*6f"), 0x6f);
  64. }
  65. END_TEST
  66. START_TEST(test_minmea_check)
  67. {
  68. for (const char **sentence=valid_sentences_nochecksum; *sentence; sentence++) {
  69. ck_assert_msg(minmea_check(*sentence, false) == true, *sentence);
  70. ck_assert_msg(minmea_check(*sentence, true) == false, *sentence);
  71. }
  72. for (const char **sentence=valid_sentences_checksum; *sentence; sentence++) {
  73. ck_assert_msg(minmea_check(*sentence, false) == true, *sentence);
  74. ck_assert_msg(minmea_check(*sentence, true) == true, *sentence);
  75. }
  76. for (const char **sentence=invalid_sentences; *sentence; sentence++) {
  77. ck_assert_msg(minmea_check(*sentence, false) == false, *sentence);
  78. ck_assert_msg(minmea_check(*sentence, true) == false, *sentence);
  79. }
  80. }
  81. END_TEST
  82. START_TEST(test_minmea_scan_c)
  83. {
  84. char ch, extra;
  85. ck_assert(minmea_scan("A,123.45", "c", &ch) == true);
  86. ck_assert_int_eq(ch, 'A');
  87. ck_assert(minmea_scan("WUT,123.45", "c", &ch) == true);
  88. ck_assert_int_eq(ch, 'W');
  89. ck_assert(minmea_scan(",123.45", "c", &ch) == true);
  90. ck_assert_int_eq(ch, '\0');
  91. ck_assert(minmea_scan("A,B", "cc", &ch, &extra) == true);
  92. ck_assert_int_eq(ch, 'A');
  93. ck_assert_int_eq(extra, 'B');
  94. ck_assert(minmea_scan("C", "cc", &ch, &extra) == false);
  95. ck_assert(minmea_scan("D", "c;c", &ch, &extra) == true);
  96. ck_assert_int_eq(ch, 'D');
  97. ck_assert_int_eq(extra, '\0');
  98. ck_assert(minmea_scan("E,F", "c;c", &ch, &extra) == true);
  99. ck_assert_int_eq(ch, 'E');
  100. ck_assert_int_eq(extra, 'F');
  101. }
  102. END_TEST
  103. START_TEST(test_minmea_scan_d)
  104. {
  105. int direction;
  106. ck_assert(minmea_scan("K", "d", &direction) == false);
  107. ck_assert(minmea_scan("", "d", &direction) == true);
  108. ck_assert(minmea_scan(",foo", "d", &direction) == true);
  109. ck_assert_int_eq(direction, 0);
  110. ck_assert(minmea_scan("N", "d", &direction) == true);
  111. ck_assert_int_eq(direction, 1);
  112. ck_assert(minmea_scan("S,foo", "d", &direction) == true);
  113. ck_assert_int_eq(direction, -1);
  114. ck_assert(minmea_scan("W", "d", &direction) == true);
  115. ck_assert_int_eq(direction, -1);
  116. ck_assert(minmea_scan("E,foo", "d", &direction) == true);
  117. ck_assert_int_eq(direction, 1);
  118. }
  119. END_TEST
  120. START_TEST(test_minmea_scan_f)
  121. {
  122. struct minmea_float f;
  123. ck_assert(minmea_scan("-", "f", &f) == false);
  124. ck_assert(minmea_scan("10-", "f", &f) == false);
  125. ck_assert(minmea_scan("+-10", "f", &f) == false);
  126. ck_assert(minmea_scan("12..45", "f", &f) == false);
  127. ck_assert(minmea_scan("blah", "f", &f) == false);
  128. ck_assert(minmea_scan("12.3.4", "f", &f) == false);
  129. ck_assert(minmea_scan(",", "f", &f) == true);
  130. ck_assert_int_eq(f.scale, 0);
  131. ck_assert(minmea_scan("", "f", &f) == true);
  132. ck_assert_int_eq(f.scale, 0);
  133. ck_assert(minmea_scan("42", "f", &f) == true);
  134. ck_assert_int_eq(f.value, 42);
  135. ck_assert_int_eq(f.scale, 1);
  136. ck_assert(minmea_scan("15.345", "f", &f) == true);
  137. ck_assert_int_eq(f.value, 15345);
  138. ck_assert_int_eq(f.scale, 1000);
  139. ck_assert(minmea_scan("-1.23,V", "f", &f) == true);
  140. ck_assert_int_eq(f.value, -123);
  141. ck_assert_int_eq(f.scale, 100);
  142. /* the guaranteed range is 32 bits which translates to +-180 degrees
  143. * with 5 decimal digits. make sure we support that. */
  144. ck_assert(minmea_scan("18000.00000", "f", &f) == true);
  145. ck_assert_int_eq(f.value, 1800000000);
  146. ck_assert_int_eq(f.scale, 100000);
  147. ck_assert(minmea_scan("-18000.00000", "f", &f) == true);
  148. ck_assert_int_eq(f.value, -1800000000);
  149. ck_assert_int_eq(f.scale, 100000);
  150. if (sizeof(int_least32_t) == 4) {
  151. /* fits in 32 bits */
  152. ck_assert(minmea_scan("2147483647", "f", &f) == true);
  153. ck_assert_int_eq(f.value, 2147483647);
  154. ck_assert_int_eq(f.scale, 1);
  155. /* doesn't fit, truncate precision */
  156. ck_assert(minmea_scan("2147483.648", "f", &f) == true);
  157. ck_assert_int_eq(f.value, 214748364);
  158. ck_assert_int_eq(f.scale, 100);
  159. /* doesn't fit, bail out */
  160. ck_assert(minmea_scan("2147483648", "f", &f) == false);
  161. } else if (sizeof(int_least32_t) == 8) {
  162. /* Casting to int64_t is ugly, but otherwise we get this on machines with 32-bit int_least32_t:
  163. * error: comparison is always false due to limited range of data type [-Werror=type-limits] */
  164. /* fits in 64 bits */
  165. ck_assert(minmea_scan("9223372036854775807", "f", &f) == true);
  166. ck_assert_int_eq((int64_t) f.value, 9223372036854775807LL);
  167. ck_assert_int_eq(f.scale, 1);
  168. /* doesn't fit, truncate precision */
  169. ck_assert(minmea_scan("9223372036854775.808", "f", &f) == true);
  170. ck_assert_int_eq((int64_t) f.value, 922337203685477580LL);
  171. ck_assert_int_eq(f.scale, 100);
  172. /* doesn't fit, bail out */
  173. ck_assert(minmea_scan("9223372036854775808", "f", &f) == false);
  174. } else {
  175. ck_abort_msg("your platform is esoteric. please fix this unit test.");
  176. }
  177. /* optional f.values */
  178. ck_assert(minmea_scan("foo", "_;f", &f) == true);
  179. ck_assert_int_eq(f.scale, 0);
  180. ck_assert(minmea_scan("foo,", "_;f", &f) == true);
  181. ck_assert_int_eq(f.scale, 0);
  182. ck_assert(minmea_scan("foo,12.3", "_;f", &f) == true);
  183. ck_assert_int_eq(f.value, 123);
  184. ck_assert_int_eq(f.scale, 10);
  185. /* accept spaces at the start of the field. some modules do this, unfortunately. */
  186. ck_assert(minmea_scan(" -1.23,V", "f", &f) == true);
  187. ck_assert_int_eq(f.value, -123);
  188. ck_assert_int_eq(f.scale, 100);
  189. ck_assert(minmea_scan(" -4.56,V", "f", &f) == true);
  190. ck_assert_int_eq(f.value, -456);
  191. ck_assert_int_eq(f.scale, 100);
  192. ck_assert(minmea_scan("-3.33 ,V", "f", &f) == false);
  193. ck_assert(minmea_scan(" -3.33 ,V", "f", &f) == false);
  194. ck_assert(minmea_scan("-3. 33,V", "f", &f) == false);
  195. ck_assert(minmea_scan("0 .0,V", "f", &f) == false);
  196. }
  197. END_TEST
  198. START_TEST(test_minmea_scan_i)
  199. {
  200. int value, extra;
  201. // valid parses
  202. ck_assert(minmea_scan("14", "i", &value) == true);
  203. ck_assert_int_eq(value, 14);
  204. ck_assert(minmea_scan("-1234", "i", &value) == true);
  205. ck_assert_int_eq(value, -1234);
  206. // empty field
  207. ck_assert(minmea_scan("", "i", &value) == true);
  208. ck_assert_int_eq(value, 0);
  209. // invalid value
  210. ck_assert(minmea_scan("foo", "i", &value) == false);
  211. // missing field
  212. ck_assert(minmea_scan("41", "ii", &value, &extra) == false);
  213. /* optional values */
  214. ck_assert(minmea_scan("10", "i;i", &value, &extra) == true);
  215. ck_assert_int_eq(value, 10);
  216. ck_assert(minmea_scan("20,30", "i;i", &value, &extra) == true);
  217. ck_assert_int_eq(value, 20);
  218. ck_assert_int_eq(extra, 30);
  219. ck_assert(minmea_scan("42,foo", "i;i", &value, &extra) == false);
  220. }
  221. END_TEST
  222. START_TEST(test_minmea_scan_s)
  223. {
  224. char value[MINMEA_MAX_LENGTH];
  225. ck_assert(minmea_scan(",bar,baz", "s", value) == true);
  226. ck_assert_str_eq(value, "");
  227. ck_assert(minmea_scan("foo,bar,baz", "s", value) == true);
  228. ck_assert_str_eq(value, "foo");
  229. ck_assert(minmea_scan("dummy", "_;s", value) == true);
  230. ck_assert_str_eq(value, "");
  231. ck_assert(minmea_scan("dummy,foo", "_;s", value) == true);
  232. ck_assert_str_eq(value, "foo");
  233. ck_assert(minmea_scan("dummy,", "_;s", value) == true);
  234. ck_assert_str_eq(value, "");
  235. }
  236. END_TEST
  237. START_TEST(test_minmea_scan_t)
  238. {
  239. char buf[7];
  240. buf[sizeof(buf)-1] = 0x42;
  241. ck_assert(minmea_scan("$GPRM,foo,bar,baz", "t", buf) == false);
  242. ck_assert(minmea_scan("GPRMC,foo,bar,baz", "t", buf) == false);
  243. ck_assert(minmea_scan("$GPRMC,foo,bar,baz", "t", buf) == true);
  244. ck_assert_str_eq(buf, "GPRMC");
  245. ck_assert(buf[sizeof(buf)-1] == 0x42);
  246. }
  247. END_TEST
  248. START_TEST(test_minmea_scan_D)
  249. {
  250. struct minmea_date d;
  251. ck_assert(minmea_scan("$GPXXX,3112", "_D", &d) == false);
  252. ck_assert(minmea_scan("$GPXXX,foobar", "_D", &d) == false);
  253. ck_assert(minmea_scan("$GPXXX,311299", "_D", &d) == true);
  254. ck_assert_int_eq(d.day, 31);
  255. ck_assert_int_eq(d.month, 12);
  256. ck_assert_int_eq(d.year, 99);
  257. ck_assert(minmea_scan("$GPXXX,,,,,,,,,nope", "_D", &d) == true);
  258. ck_assert_int_eq(d.day, -1);
  259. ck_assert_int_eq(d.month, -1);
  260. ck_assert_int_eq(d.year, -1);
  261. }
  262. END_TEST
  263. START_TEST(test_minmea_scan_T)
  264. {
  265. struct minmea_time t;
  266. ck_assert(minmea_scan("$GPXXX,2359", "_T", &t) == false);
  267. ck_assert(minmea_scan("$GPXXX,foobar", "_T", &t) == false);
  268. ck_assert(minmea_scan("$GPXXX,235960", "_T", &t) == true);
  269. ck_assert_int_eq(t.hours, 23);
  270. ck_assert_int_eq(t.minutes, 59);
  271. ck_assert_int_eq(t.seconds, 60);
  272. ck_assert_int_eq(t.microseconds, 0);
  273. ck_assert(minmea_scan("$GPXXX,213700.001", "_T", &t) == true);
  274. ck_assert_int_eq(t.hours, 21);
  275. ck_assert_int_eq(t.minutes, 37);
  276. ck_assert_int_eq(t.seconds, 0);
  277. ck_assert_int_eq(t.microseconds, 1000);
  278. ck_assert(minmea_scan("$GPXXX,,,,,,,nope", "_T", &t) == true);
  279. ck_assert_int_eq(t.hours, -1);
  280. ck_assert_int_eq(t.minutes, -1);
  281. ck_assert_int_eq(t.seconds, -1);
  282. ck_assert_int_eq(t.microseconds, -1);
  283. }
  284. END_TEST
  285. START_TEST(test_minmea_scan_complex1)
  286. {
  287. const char *sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47\r\n";
  288. char type[6];
  289. struct minmea_time t;
  290. struct minmea_float latitude; int latitude_direction;
  291. struct minmea_float longitude; int longitude_direction;
  292. int fix_quality;
  293. int satellites;
  294. struct minmea_float hdop;
  295. struct minmea_float altitude; char altitude_units;
  296. struct minmea_float height; char height_units;
  297. ck_assert(minmea_scan(sentence, "tTfdfdiiffcfc__",
  298. type,
  299. &t,
  300. &latitude, &latitude_direction,
  301. &longitude, &longitude_direction,
  302. &fix_quality,
  303. &satellites,
  304. &hdop,
  305. &altitude, &altitude_units,
  306. &height, &height_units) == true);
  307. ck_assert_str_eq(type, "GPGGA");
  308. ck_assert_int_eq(t.hours, 12);
  309. ck_assert_int_eq(t.minutes, 35);
  310. ck_assert_int_eq(t.seconds, 19);
  311. ck_assert_int_eq(latitude.value, 4807038);
  312. ck_assert_int_eq(latitude.scale, 1000);
  313. ck_assert_int_eq(latitude_direction, 1);
  314. ck_assert_int_eq(longitude.value, 1131000);
  315. ck_assert_int_eq(longitude.scale, 1000);
  316. ck_assert_int_eq(longitude_direction, 1);
  317. ck_assert_int_eq(fix_quality, 1);
  318. ck_assert_int_eq(satellites, 8);
  319. ck_assert_int_eq(hdop.value, 9);
  320. ck_assert_int_eq(hdop.scale, 10);
  321. ck_assert_int_eq(altitude.value, 5454);
  322. ck_assert_int_eq(altitude.scale, 10);
  323. ck_assert_int_eq(altitude_units, 'M');
  324. ck_assert_int_eq(height.value, 469);
  325. ck_assert_int_eq(height.scale, 10);
  326. ck_assert_int_eq(height_units, 'M');
  327. }
  328. END_TEST
  329. START_TEST(test_minmea_scan_complex2)
  330. {
  331. const char *sentence = "$GPBWC,081837,,,,,,T,,M,,N,*13";
  332. char type[6];
  333. struct minmea_time t;
  334. struct minmea_float latitude; int latitude_direction;
  335. struct minmea_float longitude; int longitude_direction;
  336. struct minmea_float bearing_true; char bearing_true_mark;
  337. struct minmea_float bearing_magnetic; char bearing_magnetic_mark;
  338. struct minmea_float distance; char distance_units;
  339. char name[MINMEA_MAX_LENGTH];
  340. ck_assert(minmea_scan(sentence, "tTfdfdfcfcfcs",
  341. type,
  342. &t,
  343. &latitude, &latitude_direction,
  344. &longitude, &longitude_direction,
  345. &bearing_true, &bearing_true_mark,
  346. &bearing_magnetic, &bearing_magnetic_mark,
  347. &distance, &distance_units,
  348. name) == true);
  349. ck_assert_str_eq(type, "GPBWC");
  350. ck_assert_int_eq(t.hours, 8);
  351. ck_assert_int_eq(t.minutes, 18);
  352. ck_assert_int_eq(t.seconds, 37);
  353. ck_assert_int_eq(latitude.scale, 0);
  354. ck_assert_int_eq(latitude_direction, 0);
  355. ck_assert_int_eq(longitude.scale, 0);
  356. ck_assert_int_eq(longitude_direction, 0);
  357. ck_assert_int_eq(bearing_true.scale, 0);
  358. ck_assert_int_eq(bearing_true_mark, 'T');
  359. ck_assert_int_eq(bearing_magnetic.scale, 0);
  360. ck_assert_int_eq(bearing_magnetic_mark, 'M');
  361. ck_assert_int_eq(distance.scale, 0);
  362. ck_assert_int_eq(distance_units, 'N');
  363. ck_assert_str_eq(name, "");
  364. }
  365. END_TEST
  366. START_TEST(test_minmea_scan_complex3)
  367. {
  368. const char *sentence = "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58";
  369. char type[6];
  370. struct minmea_time t;
  371. struct minmea_float rms_deviation;
  372. struct minmea_float semi_major_deviation;
  373. struct minmea_float semi_minor_deviation;
  374. struct minmea_float semi_major_orientation;
  375. struct minmea_float latitude_error_deviation;
  376. struct minmea_float longitude_error_deviation;
  377. struct minmea_float altitude_error_deviation;
  378. ck_assert(minmea_scan(sentence, "tTfffffff",
  379. type,
  380. &t,
  381. &rms_deviation,
  382. &semi_major_deviation,
  383. &semi_minor_deviation,
  384. &semi_major_orientation,
  385. &latitude_error_deviation,
  386. &longitude_error_deviation,
  387. &altitude_error_deviation) == true);
  388. ck_assert_str_eq(type, "GPGST");
  389. ck_assert_int_eq(t.hours, 2);
  390. ck_assert_int_eq(t.minutes, 46);
  391. ck_assert_int_eq(t.seconds, 3);
  392. ck_assert_int_eq(t.microseconds, 0);
  393. ck_assert_int_eq(rms_deviation.value, 32);
  394. ck_assert_int_eq(rms_deviation.scale, 10);
  395. ck_assert_int_eq(semi_major_deviation.value, 66);
  396. ck_assert_int_eq(semi_major_deviation.scale, 10);
  397. ck_assert_int_eq(semi_minor_deviation.value, 47);
  398. ck_assert_int_eq(semi_minor_deviation.scale, 10);
  399. ck_assert_int_eq(semi_major_orientation.value, 473);
  400. ck_assert_int_eq(semi_major_orientation.scale, 10);
  401. ck_assert_int_eq(latitude_error_deviation.value, 58);
  402. ck_assert_int_eq(latitude_error_deviation.scale, 10);
  403. ck_assert_int_eq(longitude_error_deviation.value, 56);
  404. ck_assert_int_eq(longitude_error_deviation.scale, 10);
  405. ck_assert_int_eq(altitude_error_deviation.value, 220);
  406. ck_assert_int_eq(altitude_error_deviation.scale, 10);
  407. }
  408. END_TEST
  409. START_TEST(test_minmea_parse_rmc1)
  410. {
  411. const char *sentence = "$GPRMC,081836.75,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E";
  412. struct minmea_sentence_rmc frame = {};
  413. static const struct minmea_sentence_rmc expected = {
  414. .time = { 8, 18, 36, 750000 },
  415. .valid = true,
  416. .latitude = { -375165, 100 },
  417. .longitude = { 1450736, 100 },
  418. .speed = { 0, 10 },
  419. .course = { 3600, 10 },
  420. .date = { 13, 9, 98 },
  421. .variation = { 113, 10 },
  422. };
  423. ck_assert(minmea_check(sentence, false) == true);
  424. ck_assert(minmea_check(sentence, true) == false);
  425. ck_assert(minmea_parse_rmc(&frame, sentence) == true);
  426. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  427. }
  428. END_TEST
  429. START_TEST(test_minmea_parse_rmc2)
  430. {
  431. const char *sentence = "$GPRMC,,A,3751.65,N,14507.36,W,,,,,";
  432. struct minmea_sentence_rmc frame = {};
  433. static const struct minmea_sentence_rmc expected = {
  434. .time = { -1, -1, -1, -1 },
  435. .valid = true,
  436. .latitude = { 375165, 100 },
  437. .longitude = { -1450736, 100 },
  438. .date = { -1, -1, -1 },
  439. };
  440. ck_assert(minmea_check(sentence, false) == true);
  441. ck_assert(minmea_check(sentence, true) == false);
  442. ck_assert(minmea_parse_rmc(&frame, sentence) == true);
  443. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  444. }
  445. END_TEST
  446. START_TEST(test_minmea_parse_gga1)
  447. {
  448. const char *sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47";
  449. struct minmea_sentence_gga frame = {};
  450. static const struct minmea_sentence_gga expected = {
  451. .time = { 12, 35, 19, 0 },
  452. .latitude = { 4807038, 1000 },
  453. .longitude = { 1131000, 1000 },
  454. .fix_quality = 1,
  455. .satellites_tracked = 8,
  456. .hdop = { 9, 10 },
  457. .altitude = { 5454, 10 },
  458. .altitude_units = 'M',
  459. .height = { 469, 10 },
  460. .height_units = 'M',
  461. .dgps_age = 0,
  462. };
  463. ck_assert(minmea_check(sentence, false) == true);
  464. ck_assert(minmea_check(sentence, true) == true);
  465. ck_assert(minmea_parse_gga(&frame, sentence) == true);
  466. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  467. }
  468. END_TEST
  469. START_TEST(test_minmea_parse_gst1)
  470. {
  471. const char *sentence = "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58";
  472. struct minmea_sentence_gst frame = {};
  473. struct minmea_sentence_gst expected = {
  474. .time = { 2, 46, 3, 0 },
  475. .rms_deviation = { 32, 10 },
  476. .semi_major_deviation = { 66, 10 },
  477. .semi_minor_deviation = { 47, 10 },
  478. .semi_major_orientation = { 473, 10 },
  479. .latitude_error_deviation = { 58, 10 },
  480. .longitude_error_deviation = { 56, 10 },
  481. .altitude_error_deviation = { 220, 10 },
  482. };
  483. ck_assert(minmea_check(sentence, false) == true);
  484. ck_assert(minmea_check(sentence, true) == true);
  485. ck_assert(minmea_parse_gst(&frame, sentence) == true);
  486. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  487. }
  488. END_TEST
  489. START_TEST(test_minmea_parse_gsa1)
  490. {
  491. const char *sentence = "$GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39";
  492. struct minmea_sentence_gsa frame = {};
  493. static const struct minmea_sentence_gsa expected = {
  494. .mode = MINMEA_GPGSA_MODE_AUTO,
  495. .fix_type = MINMEA_GPGSA_FIX_3D,
  496. .sats = { 4, 5, 0, 9, 12, 0, 0, 24, 0, 0, 0, 0 },
  497. .pdop = { 25, 10 },
  498. .hdop = { 13, 10 },
  499. .vdop = { 21, 10 },
  500. };
  501. ck_assert(minmea_check(sentence, false) == true);
  502. ck_assert(minmea_check(sentence, true) == true);
  503. ck_assert(minmea_parse_gsa(&frame, sentence) == true);
  504. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  505. }
  506. END_TEST
  507. START_TEST(test_minmea_parse_gll1)
  508. {
  509. const char *sentence = "$GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41";
  510. struct minmea_sentence_gll frame = {};
  511. struct minmea_sentence_gll expected = {
  512. .latitude = { 37232475, 10000 },
  513. .longitude = { -121583416, 10000 },
  514. .time = { 16, 12, 29, 487000 },
  515. .status = MINMEA_GLL_STATUS_DATA_VALID,
  516. .mode = MINMEA_GLL_MODE_AUTONOMOUS,
  517. };
  518. ck_assert(minmea_check(sentence, false) == true);
  519. ck_assert(minmea_check(sentence, true) == true);
  520. ck_assert(minmea_parse_gll(&frame, sentence) == true);
  521. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  522. }
  523. END_TEST
  524. START_TEST(test_minmea_parse_gll2)
  525. {
  526. const char *sentence = "$GPGLL,4916.45,N,12311.12,W,225444,A";
  527. struct minmea_sentence_gll frame = {};
  528. struct minmea_sentence_gll expected = {
  529. .latitude = { 491645, 100 },
  530. .longitude = { -1231112, 100 },
  531. .time = { 22, 54, 44 },
  532. .status = MINMEA_GLL_STATUS_DATA_VALID,
  533. .mode = 0,
  534. };
  535. ck_assert(minmea_check(sentence, false) == true);
  536. ck_assert(minmea_check(sentence, true) == false);
  537. ck_assert(minmea_parse_gll(&frame, sentence) == true);
  538. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  539. }
  540. END_TEST
  541. START_TEST(test_minmea_parse_gsv1)
  542. {
  543. const char *sentence = "$GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D";
  544. struct minmea_sentence_gsv frame = {};
  545. static const struct minmea_sentence_gsv expected = {
  546. .total_msgs = 3,
  547. .msg_nr = 3,
  548. .total_sats = 11,
  549. .sats = {
  550. {
  551. .nr = 22,
  552. .elevation = 42,
  553. .azimuth = 67,
  554. .snr = 42
  555. },
  556. {
  557. .nr = 24,
  558. .elevation = 14,
  559. .azimuth = 311,
  560. .snr = 43
  561. },
  562. {
  563. .nr = 27,
  564. .elevation = 5,
  565. .azimuth = 244,
  566. .snr = 0
  567. },
  568. {
  569. .nr = 0,
  570. .elevation = 0,
  571. .azimuth = 0,
  572. .snr = 0
  573. }
  574. }
  575. };
  576. ck_assert(minmea_check(sentence, false) == true);
  577. ck_assert(minmea_check(sentence, true) == true);
  578. ck_assert(minmea_parse_gsv(&frame, sentence) == true);
  579. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  580. }
  581. END_TEST
  582. START_TEST(test_minmea_parse_gsv2)
  583. {
  584. const char *sentence = "$GPGSV,4,2,11,08,51,203,30,09,45,215,28*75";
  585. struct minmea_sentence_gsv frame = {};
  586. static const struct minmea_sentence_gsv expected = {
  587. .total_msgs = 4,
  588. .msg_nr = 2,
  589. .total_sats = 11,
  590. .sats = {
  591. {
  592. .nr = 8,
  593. .elevation = 51,
  594. .azimuth = 203,
  595. .snr = 30
  596. },
  597. {
  598. .nr = 9,
  599. .elevation = 45,
  600. .azimuth = 215,
  601. .snr = 28
  602. },
  603. {
  604. .nr = 0,
  605. .elevation = 0,
  606. .azimuth = 0,
  607. .snr = 0
  608. },
  609. {
  610. .nr = 0,
  611. .elevation = 0,
  612. .azimuth = 0,
  613. .snr = 0
  614. }
  615. }
  616. };
  617. ck_assert(minmea_check(sentence, false) == true);
  618. ck_assert(minmea_check(sentence, true) == true);
  619. ck_assert(minmea_parse_gsv(&frame, sentence) == true);
  620. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  621. }
  622. END_TEST
  623. START_TEST(test_minmea_parse_gsv3)
  624. {
  625. const char *sentence = "$GPGSV,4,4,13,39,31,170,27*40";
  626. struct minmea_sentence_gsv frame = {};
  627. static const struct minmea_sentence_gsv expected = {
  628. .total_msgs = 4,
  629. .msg_nr = 4,
  630. .total_sats = 13,
  631. .sats = {
  632. {
  633. .nr = 39,
  634. .elevation = 31,
  635. .azimuth = 170,
  636. .snr = 27
  637. },
  638. {
  639. .nr = 0,
  640. .elevation = 0,
  641. .azimuth = 0,
  642. .snr = 0
  643. },
  644. {
  645. .nr = 0,
  646. .elevation = 0,
  647. .azimuth = 0,
  648. .snr = 0
  649. },
  650. {
  651. .nr = 0,
  652. .elevation = 0,
  653. .azimuth = 0,
  654. .snr = 0
  655. }
  656. }
  657. };
  658. ck_assert(minmea_check(sentence, false) == true);
  659. ck_assert(minmea_check(sentence, true) == true);
  660. ck_assert(minmea_parse_gsv(&frame, sentence) == true);
  661. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  662. }
  663. END_TEST
  664. START_TEST(test_minmea_parse_gsv4)
  665. {
  666. const char *sentence = "$GPGSV,4,4,13*7B";
  667. struct minmea_sentence_gsv frame = {};
  668. static const struct minmea_sentence_gsv expected = {
  669. .total_msgs = 4,
  670. .msg_nr = 4,
  671. .total_sats = 13,
  672. .sats = {
  673. {
  674. .nr = 0,
  675. .elevation = 0,
  676. .azimuth = 0,
  677. .snr = 0
  678. },
  679. {
  680. .nr = 0,
  681. .elevation = 0,
  682. .azimuth = 0,
  683. .snr = 0
  684. },
  685. {
  686. .nr = 0,
  687. .elevation = 0,
  688. .azimuth = 0,
  689. .snr = 0
  690. },
  691. {
  692. .nr = 0,
  693. .elevation = 0,
  694. .azimuth = 0,
  695. .snr = 0
  696. }
  697. }
  698. };
  699. ck_assert(minmea_check(sentence, false) == true);
  700. ck_assert(minmea_check(sentence, true) == true);
  701. ck_assert(minmea_parse_gsv(&frame, sentence) == true);
  702. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  703. }
  704. END_TEST
  705. START_TEST(test_minmea_parse_gsv5)
  706. {
  707. const char *sentence = "$GPGSV,4,1,13,02,28,259,33,04,12,212,27,05,34,305,30,07,79,138,*7F";
  708. struct minmea_sentence_gsv frame = {};
  709. static const struct minmea_sentence_gsv expected = {
  710. .total_msgs = 4,
  711. .msg_nr = 1,
  712. .total_sats = 13,
  713. .sats = {
  714. {
  715. .nr = 2,
  716. .elevation = 28,
  717. .azimuth = 259,
  718. .snr = 33
  719. },
  720. {
  721. .nr = 4,
  722. .elevation = 12,
  723. .azimuth = 212,
  724. .snr = 27
  725. },
  726. {
  727. .nr = 5,
  728. .elevation = 34,
  729. .azimuth = 305,
  730. .snr = 30
  731. },
  732. {
  733. .nr = 7,
  734. .elevation = 79,
  735. .azimuth = 138,
  736. .snr = 0
  737. }
  738. }
  739. };
  740. ck_assert(minmea_check(sentence, false) == true);
  741. ck_assert(minmea_check(sentence, true) == true);
  742. ck_assert(minmea_parse_gsv(&frame, sentence) == true);
  743. ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
  744. }
  745. END_TEST
  746. START_TEST(test_minmea_usage1)
  747. {
  748. const char *sentences[] = {
  749. "$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62",
  750. "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47",
  751. "$GNGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1",
  752. "$GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41",
  753. "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58",
  754. NULL,
  755. };
  756. for (const char **sentence=sentences; *sentence; sentence++) {
  757. switch (minmea_sentence_id(*sentence, false)) {
  758. case MINMEA_SENTENCE_RMC: {
  759. struct minmea_sentence_rmc frame;
  760. ck_assert(minmea_parse_rmc(&frame, *sentence) == true);
  761. } break;
  762. case MINMEA_SENTENCE_GGA: {
  763. struct minmea_sentence_gga frame;
  764. ck_assert(minmea_parse_gga(&frame, *sentence) == true);
  765. } break;
  766. case MINMEA_SENTENCE_GSA: {
  767. struct minmea_sentence_gsa frame;
  768. ck_assert(minmea_parse_gsa(&frame, *sentence) == true);
  769. } break;
  770. case MINMEA_SENTENCE_GLL: {
  771. struct minmea_sentence_gll frame;
  772. ck_assert(minmea_parse_gll(&frame, *sentence) == true);
  773. } break;
  774. case MINMEA_SENTENCE_GST: {
  775. struct minmea_sentence_gst frame;
  776. ck_assert(minmea_parse_gst(&frame, *sentence) == true);
  777. } break;
  778. default: {
  779. } break;
  780. }
  781. char talker[3];
  782. char expected[3] = { (*sentence)[1], (*sentence)[2], '\0' };
  783. ck_assert(minmea_talker_id(talker, *sentence) == true);
  784. ck_assert_str_eq(talker, expected);
  785. }
  786. }
  787. END_TEST
  788. START_TEST(test_minmea_gettime)
  789. {
  790. struct minmea_date d = { 14, 2, 14 };
  791. struct minmea_time t = { 13, 0, 9, 123456 };
  792. struct timespec ts;
  793. ck_assert(minmea_gettime(&ts, &d, &t) == 0);
  794. ck_assert_int_eq(ts.tv_sec, 1392382809);
  795. ck_assert_int_eq(ts.tv_nsec, 123456000);
  796. d.year = -1;
  797. ck_assert(minmea_gettime(&ts, &d, &t) != 0);
  798. d.year = 2014;
  799. t.hours = -1;
  800. ck_assert(minmea_gettime(&ts, &d, &t) != 0);
  801. }
  802. END_TEST
  803. START_TEST(test_minmea_rescale)
  804. {
  805. /* basic and edge cases. */
  806. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 42, 0 }, 3), 0);
  807. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1234, 10 }, 1), 123);
  808. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1235, 10 }, 1), 124);
  809. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1234, 10 }, 1000), 123400);
  810. /* round towards zero. */
  811. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1234, 10 }, 1), -123);
  812. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1235, 10 }, 1), -124);
  813. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1236, 10 }, 1), -124);
  814. /* shouldn't overflow on large numbers. */
  815. ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 510693608, 100000 }, 10000), 51069361);
  816. }
  817. END_TEST
  818. /* The float values used in tests should be exactly representable under IEEE754;
  819. * false negatives will occur otherwise. */
  820. #define assert_float_eq(x, y) ck_assert(fabsf((x) - (y)) <= 0.0f)
  821. START_TEST(test_minmea_float)
  822. {
  823. ck_assert(isnan(minmea_tofloat(&(struct minmea_float) { 42, 0 })));
  824. assert_float_eq(minmea_tofloat(&(struct minmea_float) { 7, 1 }), 7.0f);
  825. assert_float_eq(minmea_tofloat(&(struct minmea_float) { -200, 100 }), -2.0f);
  826. assert_float_eq(minmea_tofloat(&(struct minmea_float) { 15, 10 }), 1.5f);
  827. }
  828. END_TEST
  829. START_TEST(test_minmea_coord)
  830. {
  831. ck_assert(isnan(minmea_tocoord(&(struct minmea_float) { 42, 0 })));
  832. assert_float_eq(minmea_tocoord(&(struct minmea_float) { 4200, 1 }), 42.0f);
  833. assert_float_eq(minmea_tocoord(&(struct minmea_float) { 420000, 100 }), 42.0f);
  834. assert_float_eq(minmea_tocoord(&(struct minmea_float) { 423000, 100 }), 42.5f);
  835. }
  836. END_TEST
  837. static Suite *minmea_suite(void)
  838. {
  839. Suite *s = suite_create ("minmea");
  840. TCase *tc_checksum = tcase_create("minmea_checksum");
  841. tcase_add_test(tc_checksum, test_minmea_checksum);
  842. suite_add_tcase(s, tc_checksum);
  843. TCase *tc_check = tcase_create("minmea_check");
  844. tcase_add_test(tc_check, test_minmea_check);
  845. suite_add_tcase(s, tc_check);
  846. TCase *tc_scan = tcase_create("minmea_scan");
  847. tcase_add_test(tc_scan, test_minmea_scan_c);
  848. tcase_add_test(tc_scan, test_minmea_scan_d);
  849. tcase_add_test(tc_scan, test_minmea_scan_f);
  850. tcase_add_test(tc_scan, test_minmea_scan_i);
  851. tcase_add_test(tc_scan, test_minmea_scan_s);
  852. tcase_add_test(tc_scan, test_minmea_scan_t);
  853. tcase_add_test(tc_scan, test_minmea_scan_D);
  854. tcase_add_test(tc_scan, test_minmea_scan_T);
  855. tcase_add_test(tc_scan, test_minmea_scan_complex1);
  856. tcase_add_test(tc_scan, test_minmea_scan_complex2);
  857. tcase_add_test(tc_scan, test_minmea_scan_complex3);
  858. suite_add_tcase(s, tc_scan);
  859. TCase *tc_parse = tcase_create("minmea_parse");
  860. tcase_add_test(tc_parse, test_minmea_parse_rmc1);
  861. tcase_add_test(tc_parse, test_minmea_parse_rmc2);
  862. tcase_add_test(tc_parse, test_minmea_parse_gga1);
  863. tcase_add_test(tc_parse, test_minmea_parse_gsa1);
  864. tcase_add_test(tc_parse, test_minmea_parse_gll1);
  865. tcase_add_test(tc_parse, test_minmea_parse_gll2);
  866. tcase_add_test(tc_parse, test_minmea_parse_gst1);
  867. tcase_add_test(tc_parse, test_minmea_parse_gsv1);
  868. tcase_add_test(tc_parse, test_minmea_parse_gsv2);
  869. tcase_add_test(tc_parse, test_minmea_parse_gsv3);
  870. tcase_add_test(tc_parse, test_minmea_parse_gsv4);
  871. tcase_add_test(tc_parse, test_minmea_parse_gsv5);
  872. suite_add_tcase(s, tc_parse);
  873. TCase *tc_usage = tcase_create("minmea_usage");
  874. tcase_add_test(tc_usage, test_minmea_usage1);
  875. suite_add_tcase(s, tc_usage);
  876. TCase *tc_utils = tcase_create("minmea_utils");
  877. tcase_add_test(tc_utils, test_minmea_gettime);
  878. tcase_add_test(tc_utils, test_minmea_rescale);
  879. tcase_add_test(tc_utils, test_minmea_float);
  880. tcase_add_test(tc_utils, test_minmea_coord);
  881. suite_add_tcase(s, tc_utils);
  882. return s;
  883. }
  884. int main(void)
  885. {
  886. int number_failed;
  887. Suite *s = minmea_suite();
  888. SRunner *sr = srunner_create(s);
  889. srunner_run_all(sr, CK_NORMAL);
  890. number_failed = srunner_ntests_failed(sr);
  891. srunner_free(sr);
  892. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  893. }
  894. /* vim: set ts=4 sw=4 et: */