1 /*
2  * Copyright 2023 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 
9 #include "gnss_parse.h"
10 
11 struct test_atoi_sample {
12 	const char *str;
13 	uint8_t base;
14 	int32_t value;
15 };
16 
17 static const struct test_atoi_sample atoi_samples[] = {
18 	{.str = "10", .base = 10, .value = 10},
19 	{.str = "1", .base = 10, .value = 1},
20 	{.str = "002", .base = 10, .value = 2},
21 	{.str = "-10", .base = 10, .value = -10},
22 	{.str = "-1", .base = 10, .value = -1},
23 	{.str = "-002", .base = 10, .value = -2},
24 	{.str = "30000000", .base = 10, .value = 30000000},
25 	{.str = "-30000000", .base = 10, .value = -30000000},
26 	{.str = "00", .base = 16, .value = 0},
27 	{.str = "20", .base = 16, .value = 32},
28 	{.str = "42", .base = 16, .value = 66},
29 	{.str = "122", .base = 16, .value = 290},
30 	{.str = "0122", .base = 16, .value = 290},
31 };
32 
ZTEST(gnss_parse,test_atoi)33 ZTEST(gnss_parse, test_atoi)
34 {
35 	int32_t value;
36 
37 	for (size_t i = 0; i < ARRAY_SIZE(atoi_samples); i++) {
38 		zassert_ok(gnss_parse_atoi(atoi_samples[i].str, atoi_samples[i].base, &value),
39 			   "Parse failed");
40 
41 		zassert_equal(atoi_samples[i].value, value, "Parsed value is incorrect");
42 	}
43 
44 	zassert_equal(gnss_parse_atoi("a10", 10, &value), -EINVAL,
45 		      "Parse should fail due to invalid base 10 chars");
46 
47 	zassert_equal(gnss_parse_atoi("h#1c", 16, &value), -EINVAL,
48 		      "Parse should fail due to invalid base 16 chars");
49 }
50 
51 struct test_dec_sample {
52 	const char *str;
53 	int64_t value;
54 };
55 
56 static const struct test_dec_sample dec_to_nano_samples[] = {
57 	{.str = "10", .value = 10000000000},
58 	{.str = "1", .value = 1000000000},
59 	{.str = "002", .value = 2000000000},
60 	{.str = "-10", .value = -10000000000},
61 	{.str = "-1", .value = -1000000000},
62 	{.str = "-002", .value = -2000000000},
63 	{.str = "30000000", .value = 30000000000000000},
64 	{.str = "-30000000", .value = -30000000000000000},
65 	{.str = "0.10", .value = 100000000},
66 	{.str = "-0.10", .value = -100000000},
67 	{.str = "1", .value = 1000000000},
68 	{.str = "002.000", .value = 2000000000},
69 	{.str = "-002.000", .value = -2000000000},
70 	{.str = "0.989812343", .value = 989812343},
71 	{.str = "-0.989812343", .value = -989812343},
72 	{.str = "0.112211", .value = 112211000},
73 	{.str = "-0.112211", .value = -112211000},
74 	{.str = "000000000.112211000000000000", .value = 112211000},
75 	{.str = "-000000000.11221100000000000", .value = -112211000},
76 };
77 
ZTEST(gnss_parse,test_dec_to_nano)78 ZTEST(gnss_parse, test_dec_to_nano)
79 {
80 	int64_t value;
81 
82 	for (volatile size_t i = 0; i < ARRAY_SIZE(dec_to_nano_samples); i++) {
83 		zassert_ok(gnss_parse_dec_to_nano(dec_to_nano_samples[i].str, &value),
84 			   "Parse failed");
85 
86 		zassert_equal(dec_to_nano_samples[i].value, value, "Parsed value is incorrect");
87 	}
88 
89 	zassert_equal(gnss_parse_dec_to_nano("-0s02..000", &value), -EINVAL,
90 		      "Parse should fail due to double dot");
91 
92 	zassert_equal(gnss_parse_dec_to_nano("--002.000", &value), -EINVAL,
93 		      "Parse should fail due to double -");
94 
95 	zassert_equal(gnss_parse_dec_to_nano("-00s2.000", &value), -EINVAL,
96 		      "Parse should fail due to invalid char");
97 }
98 
99 ZTEST_SUITE(gnss_parse, NULL, NULL, NULL, NULL, NULL);
100