CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Common/tests/test_ap_common.cpp
Views: 1799
1
#include <AP_gtest.h>
2
3
#include <AP_Common/AP_Common.h>
4
5
TEST(AP_Common, HexToUint8)
6
{
7
uint8_t res;
8
for (uint8_t test_value = '0', expected_res = 0; test_value < ':'; test_value++, expected_res++) {
9
EXPECT_TRUE(hex_to_uint8(test_value, res));
10
EXPECT_EQ(expected_res, res);
11
}
12
for (uint8_t test_value = 'A', expected_res = 10; test_value < 'G'; test_value++, expected_res++) {
13
EXPECT_TRUE(hex_to_uint8(test_value, res));
14
EXPECT_EQ(expected_res, res);
15
}
16
for (uint8_t test_value = 'a', expected_res = 10; test_value < 'g'; test_value++, expected_res++) {
17
EXPECT_TRUE(hex_to_uint8(test_value, res));
18
EXPECT_EQ(expected_res, res);
19
}
20
EXPECT_FALSE(hex_to_uint8('G', res));
21
EXPECT_FALSE(hex_to_uint8('g', res));
22
EXPECT_FALSE(hex_to_uint8(';', res));
23
EXPECT_FALSE(hex_to_uint8('/', res));
24
EXPECT_FALSE(hex_to_uint8('@', res));
25
EXPECT_FALSE(hex_to_uint8('`', res));
26
}
27
28
TEST(AP_Common, BoundedInt32)
29
{
30
EXPECT_TRUE(is_bounded_int32(1, 0, 2));
31
EXPECT_FALSE(is_bounded_int32(3, 0, 2));
32
EXPECT_FALSE(is_bounded_int32(-1, 0, 2));
33
EXPECT_TRUE(is_bounded_int32(0, -1, 2));
34
EXPECT_TRUE(is_bounded_int32(-1, -2, 0));
35
}
36
37
TEST(AP_Common, BitSet)
38
{
39
uint16_t test_value1 = 128;
40
BIT_SET(test_value1, 3);
41
EXPECT_EQ(test_value1, 136u);
42
BIT_CLEAR(test_value1, 7);
43
EXPECT_EQ(test_value1, 8u);
44
45
uint32_t test_value2 = 128;
46
BIT_SET(test_value2, 3);
47
EXPECT_EQ(test_value2, 136u);
48
BIT_CLEAR(test_value2, 7);
49
EXPECT_EQ(test_value2, 8u);
50
51
unsigned long test_value3 = 128;
52
BIT_SET(test_value3, 3);
53
EXPECT_EQ(test_value3, 136u);
54
BIT_CLEAR(test_value3, 7);
55
EXPECT_EQ(test_value3, 8u);
56
}
57
58
59
TEST(AP_Common, StrcpyNoTerm)
60
{
61
const char src[] = "This is ArduPilot";
62
char dest[ARRAY_SIZE(src)-5]{};
63
64
strncpy_noterm(dest, src, 12);
65
EXPECT_STRNE(dest, src);
66
EXPECT_STREQ("This is Ardu", dest);
67
68
const char src2[] = "ArduPilot";
69
char dest2[ARRAY_SIZE(src)-5]{};
70
strncpy_noterm(dest2, src2, 12);
71
EXPECT_STREQ(dest2, src2);
72
}
73
AP_GTEST_MAIN()
74
75