xref: /btstack/test/btstack_util/btstack_util_test.cpp (revision 65e76442879710536c8282d7434f54986ceda717)
1 
2 #include "CppUTest/TestHarness.h"
3 #include "CppUTest/CommandLineTestRunner.h"
4 
5 #include "btstack_util.h"
6 #include "btstack_config.h"
7 #include <stdio.h>
8 
9 static bool assert_failed;
10 
11 void test_assert(bool condition){
12     if (condition != true){
13         assert_failed = true;
14     }
15 }
16 
17 static void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
18 	int i;
19 	for (i=0; i<size; i++){
20 		if (expected[i] != actual[i]) {
21 			printf("offset %u wrong\n", i);
22 			printf("expected: "); printf_hexdump(expected, size);
23 			printf("actual:   "); printf_hexdump(actual, size);
24 		}
25 		BYTES_EQUAL(expected[i], actual[i]);
26 	}
27 }
28 
29 TEST_GROUP(btstack_util){
30     char buffer[8];
31     void setup(void){
32         assert_failed = false;
33     }
34 };
35 
36 TEST(btstack_util, btstack_printf_strlen){
37     CHECK_EQUAL(4, btstack_printf_strlen("%04x", 0x1234));
38 }
39 
40 TEST(btstack_util, btstack_snprintf_assert_complete_ok){
41     uint16_t len = btstack_snprintf_assert_complete(buffer, sizeof(buffer), "%04x", 0x1234);
42     CHECK_EQUAL(4, len);
43     CHECK_EQUAL_ARRAY((uint8_t*) "1234", (uint8_t*) buffer, 4);
44 }
45 
46 TEST(btstack_util, btstack_snprintf_assert_complete_too_long){
47     (void) btstack_snprintf_assert_complete(buffer, sizeof(buffer), "%08x", 0x1234);
48     CHECK_TRUE(assert_failed);
49 }
50 
51 TEST(btstack_util, btstack_snprintf_best_effort_complete){
52     uint16_t len = btstack_snprintf_best_effort(buffer, sizeof(buffer), "%04x", 0x1234);
53     CHECK_EQUAL(4, len);
54     CHECK_EQUAL_ARRAY((uint8_t*) "1234", (uint8_t*) buffer, 4);
55 }
56 
57 TEST(btstack_util, btstack_snprintf_best_effort_incomplete){
58     uint16_t len = btstack_snprintf_best_effort(buffer, sizeof(buffer), "%08x", 0x1234);
59     CHECK_EQUAL(7, len);
60     CHECK_EQUAL_ARRAY((uint8_t*) "0000000", (uint8_t*) buffer, 4);
61 }
62 
63 int main (int argc, const char * argv[]){
64     return CommandLineTestRunner::RunAllTests(argc, argv);
65 }
66