Skip to content

Commit 48c4530

Browse files
author
Adil Benhlal
committed
test(cbor): cbor_encode_array
1 parent f298f73 commit 48c4530

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ add_executable(test_cb_ops "${CMAKE_SOURCE_DIR}/tests/test_cb_ops.c" "${CMAKE_SO
4444
add_test(NAME test_cb_ops COMMAND test_cb_ops)
4545
add_executable(test_cb_cbor_string "${CMAKE_SOURCE_DIR}/tests/common/cbor/test_cb_cbor_string.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor_string.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor_simple.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor_int.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor.c" "${CMAKE_SOURCE_DIR}/src/common/cb_endianness.c")
4646
add_test(NAME test_cb_cbor_string COMMAND test_cb_cbor_string)
47+
add_executable(test_cb_cbor_array "${CMAKE_SOURCE_DIR}/tests/common/cbor/test_cb_cbor_array.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor_array.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor_int.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor.c" "${CMAKE_SOURCE_DIR}/src/common/cbor/cb_cbor_simple.c" "${CMAKE_SOURCE_DIR}/src/common/cb_endianness.c")
48+
add_test(NAME test_cb_cbor_array COMMAND test_cb_cbor_array)
4749

4850
# INSTALL CBORG
4951
install(TARGETS cborg DESTINATION ${CMAKE_INSTALL_BINDIR})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2021-present Adil Benhlal <[email protected]>
3+
*
4+
*/
5+
6+
#include <assert.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
#include "common/cbor/cb_cbor_array.h"
12+
#include "common/cbor/cb_cbor_int.h"
13+
14+
void test_cb_cbor_array() {
15+
uint8_t ev_array[4] = {0xFF, 0xFF, 0xFF, 0xFF}; // encoded value
16+
uint8_t expect_array[4] = {0x83, 0x0E, 0x00, 0x17}; // [ 0x83, 0x0E, 0x00, 0x17 ]
17+
assert(cb_cbor_encode_array_start(ev_array, 3, 4) == 1);
18+
assert(cb_cbor_encode_uint(14, ev_array + 1, 3) == 1);
19+
assert(cb_cbor_encode_uint(0, ev_array + 2, 2) == 1);
20+
assert(cb_cbor_encode_uint(23, ev_array + 3, 1) == 1);
21+
assert(memcmp(expect_array, ev_array, 4) == 0);
22+
}
23+
24+
void test_cb_cbor_array_indef() {
25+
uint8_t ev_array_indef[5] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA}; // encoded value
26+
uint8_t expect_array_indef[5] = {0x9F, 0x0E, 0x00, 0x17, 0xFF}; // [ 0x9F, 0x0E, 0x00, 0x17, 0xFF ]
27+
assert(cb_cbor_encode_array_start_indef(ev_array_indef, 5) == 1);
28+
assert(cb_cbor_encode_uint(14, ev_array_indef + 1, 4) == 1);
29+
assert(cb_cbor_encode_uint(0, ev_array_indef + 2, 3) == 1);
30+
assert(cb_cbor_encode_uint(23, ev_array_indef + 3, 2) == 1);
31+
assert(cb_cbor_encode_array_stop_indef(ev_array_indef + 4, 1) == 1);
32+
assert(memcmp(expect_array_indef, ev_array_indef, 5) == 0);
33+
}
34+
35+
// TODO: criterion or cmocka
36+
int main() {
37+
test_cb_cbor_array();
38+
test_cb_cbor_array_indef();
39+
40+
return EXIT_SUCCESS;
41+
}

0 commit comments

Comments
 (0)