Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: cleanup of code #72

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/automated/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ static int add_tests(CU_pSuite pSuite, const test_t *tests)
int main()
{
CU_pSuite pSuite = NULL;
if (CUE_SUCCESS != CU_initialize_registry())
if (CUE_SUCCESS != CU_initialize_registry()) {
return CU_get_error();
}
pSuite = CU_add_suite("Nanocbor decode", NULL, NULL);
if (NULL == pSuite) {
CU_cleanup_registry();
Expand Down
16 changes: 10 additions & 6 deletions tests/automated/test_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "test.h"
#include <CUnit/CUnit.h>

/* NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers) */

static void test_decode_indefinite(void)
{
/* Test vector, 3 integers in an indefinite array */
Expand Down Expand Up @@ -172,7 +174,7 @@ static void test_decode_none(void)
{
nanocbor_value_t val;
nanocbor_value_t cont;
uint64_t tmp;
uint64_t tmp = 0;
nanocbor_decoder_init(&val, NULL, 0);

CU_ASSERT_EQUAL(nanocbor_get_type(&val), NANOCBOR_ERR_END);
Expand Down Expand Up @@ -207,12 +209,12 @@ static void test_decode_basic(void)
CU_ASSERT_EQUAL(5, intval);

const uint8_t decimal_frac[] = { 0xC4, 0x82, 0x21, 0x19, 0x6a, 0xb3 };
int32_t m;
int32_t e;
int32_t mantissa = 0;
int32_t exponent = 0;
nanocbor_decoder_init(&decoder, decimal_frac, sizeof(decimal_frac));
CU_ASSERT_EQUAL(nanocbor_get_decimal_frac(&decoder, &e, &m), 0);
CU_ASSERT_EQUAL(e, -2);
CU_ASSERT_EQUAL(m, 27315);
CU_ASSERT_EQUAL(nanocbor_get_decimal_frac(&decoder, &exponent, &mantissa), 0);
CU_ASSERT_EQUAL(exponent, -2);
CU_ASSERT_EQUAL(mantissa, 27315);
}

const test_t tests_decoder[] = {
Expand Down Expand Up @@ -245,3 +247,5 @@ const test_t tests_decoder[] = {
.n = NULL,
},
};

/* NOLINTEND(cppcoreguidelines-avoid-magic-numbers) */
3 changes: 3 additions & 0 deletions tests/automated/test_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <float.h>
#include <math.h>


static void print_bytestr(const uint8_t *bytes, size_t len)
{
printf("\n");
Expand Down Expand Up @@ -39,6 +40,7 @@ static void test_encode_float_specials(void)

static void test_encode_float_to_half(void)
{
// NOLINTBEGIN
uint8_t buf[64];
nanocbor_encoder_t enc;
nanocbor_encoder_init(&enc, buf, sizeof(buf));
Expand All @@ -53,6 +55,7 @@ static void test_encode_float_to_half(void)
CU_ASSERT_EQUAL(nanocbor_fmt_float(&enc, -1.9990234375), 3);
CU_ASSERT_EQUAL(nanocbor_fmt_float(&enc, -1.99951171875), 5);
CU_ASSERT_EQUAL(nanocbor_fmt_float(&enc, -2.0009765625), 5);
// NOLINTEND

nanocbor_fmt_end_indefinite(&enc);
print_bytestr(buf, nanocbor_encoded_len(&enc));
Expand Down
6 changes: 3 additions & 3 deletions tests/encode/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* SPDX-License-Identifier: CC0-1.0
*/

#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

Expand All @@ -25,7 +25,7 @@ static void _encode(nanocbor_encoder_t *enc)
nanocbor_fmt_int(enc, -500);
nanocbor_put_tstr(enc, "this is a long string");
nanocbor_fmt_float(enc, 0.34);
nanocbor_put_bstr(enc, (uint8_t*)"bytez", sizeof("bytez"));
nanocbor_put_bstr(enc, (uint8_t *)"bytez", sizeof("bytez"));
nanocbor_fmt_null(enc);
nanocbor_fmt_decimal_frac(enc, -2, 27315);
nanocbor_fmt_end_indefinite(enc);
Expand All @@ -48,7 +48,7 @@ int main(void)
nanocbor_encoder_init(&enc, buf, required);
_encode(&enc);

//printf("Bytes: %u\n", (unsigned)nanocbor_encoded_len(&enc));
// printf("Bytes: %u\n", (unsigned)nanocbor_encoded_len(&enc));
fwrite(buf, 1, nanocbor_encoded_len(&enc), stdout);

return 0;
Expand Down