Skip to content

Commit afda850

Browse files
hlovdalianfixes
authored andcommitted
Add unit test for itoa
1 parent 1ec9501 commit afda850

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <ArduinoUnitTests.h>
2+
#include <Arduino.h>
3+
4+
#define ARRAY_SIZEOF(a) ( sizeof(a) / sizeof((a)[0]) )
5+
6+
unittest(library_tests_itoa)
7+
{
8+
char buf[32];
9+
const char *result;
10+
struct {
11+
int value;
12+
const char *expected;
13+
int base;
14+
} table[] = {
15+
{ 54325, "1101010000110101", 2 },
16+
{ 54325, "54325", 10 },
17+
{ 54325, "D435", 16 },
18+
{ 493, "755", 8 },
19+
{ -1, "-1", 10 },
20+
{ 32767, "32767", 10},
21+
{ 32767, "7FFF", 16},
22+
{ 65535, "65535", 10},
23+
{ 65535, "FFFF", 16},
24+
{ 2147483647, "2147483647", 10},
25+
{ 2147483647, "7FFFFFFF", 16},
26+
};
27+
28+
for (int i = 0; i < ARRAY_SIZEOF(table); i++) {
29+
result = itoa(table[i].value, buf, table[i].base);
30+
assertEqual(table[i].expected, result);
31+
}
32+
33+
// While only bases 2, 8, 10 and 16 are of real interest, lets test that all
34+
// bases at least produce expected output for a few test points simple to test.
35+
for (int base = 2; base <= 16; base++) {
36+
result = itoa(0, buf, base);
37+
assertEqual("0", result);
38+
result = itoa(1, buf, base);
39+
assertEqual("1", result);
40+
result = itoa(base, buf, base);
41+
assertEqual("10", result);
42+
}
43+
44+
}
45+
46+
unittest_main()

0 commit comments

Comments
 (0)