|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <ztest.h> |
| 8 | +#include <sys/cbprintf.h> |
| 9 | + |
| 10 | +#define CBPRINTF_DEBUG 1 |
| 11 | + |
| 12 | +struct out_buffer { |
| 13 | + char *buf; |
| 14 | + size_t idx; |
| 15 | + size_t size; |
| 16 | +}; |
| 17 | + |
| 18 | +static int out(int c, void *dest) |
| 19 | +{ |
| 20 | + int rv = EOF; |
| 21 | + struct out_buffer *buf = dest; |
| 22 | + |
| 23 | + if (buf->idx < buf->size) { |
| 24 | + buf->buf[buf->idx++] = (char)(unsigned char)c; |
| 25 | + rv = (int)(unsigned char)c; |
| 26 | + } |
| 27 | + return rv; |
| 28 | +} |
| 29 | + |
| 30 | +static char runtime_buf[128]; |
| 31 | +static char compare_buf[128]; |
| 32 | + |
| 33 | +void dump(const char *desc, uint8_t *package, size_t len) |
| 34 | +{ |
| 35 | + printk("%s package %p:\n", desc, package); |
| 36 | + for (size_t i = 0; i < len; i++) { |
| 37 | + printk("%02x ", package[i]); |
| 38 | + } |
| 39 | + printk("\n"); |
| 40 | +} |
| 41 | + |
| 42 | +void unpack(const char *desc, struct out_buffer *buf, |
| 43 | + uint8_t *package, size_t len) |
| 44 | +{ |
| 45 | + cbpprintf(out, buf, package); |
| 46 | + buf->buf[buf->idx] = 0; |
| 47 | + zassert_equal(strcmp(buf->buf, compare_buf), 0, |
| 48 | + "Strings differ\nexp: |%s|\ngot: |%s|\n", |
| 49 | + compare_buf, buf->buf); |
| 50 | +} |
| 51 | + |
| 52 | +#define TEST_PACKAGING(fmt, ...) do { \ |
| 53 | + snprintf(compare_buf, sizeof(compare_buf), fmt, __VA_ARGS__); \ |
| 54 | + printk("-----------------------------------------\n"); \ |
| 55 | + printk("%s\n", compare_buf); \ |
| 56 | + struct out_buffer rt_buf = { \ |
| 57 | + .buf = runtime_buf, .idx = 0, .size = sizeof(runtime_buf) \ |
| 58 | + }; \ |
| 59 | + int rc = cbprintf_package(NULL, 0, fmt, __VA_ARGS__); \ |
| 60 | + zassert_true(rc > 0, "cbprintf_package() returned %d", rc); \ |
| 61 | + size_t len = rc; \ |
| 62 | + uint8_t __aligned(8) rt_package[len]; \ |
| 63 | + memset(rt_package, 0, len); \ |
| 64 | + rc = cbprintf_package(rt_package, len, fmt, __VA_ARGS__); \ |
| 65 | + zassert_equal(rc, len, "cbprintf_package() returned %d, expected %d", rc, len); \ |
| 66 | + dump("runtime", rt_package, len); \ |
| 67 | + unpack("runtime", &rt_buf, rt_package, len); \ |
| 68 | +} while (0) |
| 69 | + |
| 70 | +void test_cbprintf_package(void) |
| 71 | +{ |
| 72 | + volatile signed char sc = -11; |
| 73 | + int i = 100; |
| 74 | + char c = 'a'; |
| 75 | + static const short s = -300; |
| 76 | + long li = -1111111111; |
| 77 | + long long lli = 0x1122334455667788; |
| 78 | + unsigned char uc = 100; |
| 79 | + unsigned int ui = 0x12345; |
| 80 | + unsigned short us = 0x1234; |
| 81 | + unsigned long ul = 0xaabbaabb; |
| 82 | + unsigned long long ull = 0xaabbaabbaabb; |
| 83 | + float f = -1.234; |
| 84 | + double d = 1.2333; |
| 85 | + |
| 86 | + /* tests to exercize different element alignments */ |
| 87 | + TEST_PACKAGING("test long %x %lx %x", 0xb1b2b3b4, li, 0xe4e3e2e1); |
| 88 | + TEST_PACKAGING("test long long %x %llx %x", 0xb1b2b3b4, lli, 0xe4e3e2e1); |
| 89 | + if (IS_ENABLED(CONFIG_CBPRINTF_FP_SUPPORT)) { |
| 90 | + TEST_PACKAGING("test double %x %f %x", 0xb1b2b3b4, d, 0xe4e3e2e1); |
| 91 | + } |
| 92 | + |
| 93 | + /* tests with varied elements */ |
| 94 | + TEST_PACKAGING("test %d %hd %hhd", i, s, sc); |
| 95 | + TEST_PACKAGING("test %ld %llx %hhu %hu %u", li, lli, uc, us, ui); |
| 96 | + TEST_PACKAGING("test %lu %llu", ul, ull); |
| 97 | + TEST_PACKAGING("test %c %p", c, &c); |
| 98 | + if (IS_ENABLED(CONFIG_CBPRINTF_FP_SUPPORT)) { |
| 99 | + TEST_PACKAGING("test %f %a", f, d); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void test_main(void) |
| 104 | +{ |
| 105 | + printk("sizeof: int=%zu long=%zu ptr=%zu long long=%zu double=%zu long double=%zu\n", |
| 106 | + sizeof(int), sizeof(long), sizeof(void *), sizeof(long long), |
| 107 | + sizeof(double), sizeof(long double)); |
| 108 | + printk("alignof: int=%zu long=%zu ptr=%zu long long=%zu double=%zu long double=%zu\n", |
| 109 | + __alignof__(int), __alignof__(long), __alignof__(void *), |
| 110 | + __alignof__(long long), __alignof__(double), __alignof__(long double)); |
| 111 | + |
| 112 | + ztest_test_suite(cbprintf_package, |
| 113 | + ztest_unit_test(test_cbprintf_package) |
| 114 | + ); |
| 115 | + |
| 116 | + ztest_run_test_suite(cbprintf_package); |
| 117 | +} |
0 commit comments