From 5eeaed09aed64b088cbc7cf946923aeea92e4f67 Mon Sep 17 00:00:00 2001 From: Yu-Chen Lin Date: Sat, 30 Nov 2019 12:15:58 +0800 Subject: [PATCH 1/3] Add test_strquote Signed-off-by: Yu-Chen Lin --- app/tests/test_strutil.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/tests/test_strutil.c b/app/tests/test_strutil.c index 64e62ed1..1489b1bd 100644 --- a/app/tests/test_strutil.c +++ b/app/tests/test_strutil.c @@ -1,5 +1,6 @@ #include #include +#include #include "util/str_util.h" @@ -126,6 +127,16 @@ static void test_xstrjoin_truncated_after_sep(void) { assert(!strcmp("abc de ", s)); } +static void test_strquote(void) { + const char *s = "abcde"; + char *out = strquote(s); + + // add '"' at the beginning and the end + assert(!strcmp("\"abcde\"", out)); + + SDL_free(out); +} + static void test_utf8_truncate(void) { const char *s = "aÉbÔc"; assert(strlen(s) == 7); // É and Ô are 2 bytes-wide @@ -166,6 +177,7 @@ int main(void) { test_xstrjoin_truncated_in_token(); test_xstrjoin_truncated_before_sep(); test_xstrjoin_truncated_after_sep(); + test_strquote(); test_utf8_truncate(); return 0; } From b2bf25c52ca08d8de812186ab7651128e9989cb3 Mon Sep 17 00:00:00 2001 From: Yu-Chen Lin Date: Sat, 30 Nov 2019 12:33:00 +0800 Subject: [PATCH 2/3] Add test_buffer_util Signed-off-by: Yu-Chen Lin --- app/meson.build | 3 ++ app/tests/test_buffer_util.c | 76 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 app/tests/test_buffer_util.c diff --git a/app/meson.build b/app/meson.build index 843bfd9d..332407c1 100644 --- a/app/meson.build +++ b/app/meson.build @@ -141,6 +141,9 @@ install_man('scrcpy.1') ### TESTS tests = [ + ['test_buffer_util', [ + 'tests/test_buffer_util.c' + ]], ['test_cbuf', [ 'tests/test_cbuf.c', ]], diff --git a/app/tests/test_buffer_util.c b/app/tests/test_buffer_util.c new file mode 100644 index 00000000..ba3f9f06 --- /dev/null +++ b/app/tests/test_buffer_util.c @@ -0,0 +1,76 @@ +#include + +#include "util/buffer_util.h" + +static void test_buffer_write16be(void) { + uint16_t val = 0xABCD; + uint8_t buf[2]; + + buffer_write16be(buf, val); + + assert(buf[0] == 0xAB); + assert(buf[1] == 0xCD); +} + +static void test_buffer_write32be(void) { + uint32_t val = 0xABCD1234; + uint8_t buf[4]; + + buffer_write32be(buf, val); + + assert(buf[0] == 0xAB); + assert(buf[1] == 0xCD); + assert(buf[2] == 0x12); + assert(buf[3] == 0x34); +} + +static void test_buffer_write64be(void) { + uint64_t val = 0xABCD1234567890EF; + uint8_t buf[8]; + + buffer_write64be(buf, val); + + assert(buf[0] == 0xAB); + assert(buf[1] == 0xCD); + assert(buf[2] == 0x12); + assert(buf[3] == 0x34); + assert(buf[4] == 0x56); + assert(buf[5] == 0x78); + assert(buf[6] == 0x90); + assert(buf[7] == 0xEF); +} + +static void test_buffer_read16be(void) { + uint8_t buf[2] = {0xAB, 0xCD}; + + uint16_t val = buffer_read16be(buf); + + assert(val == 0xABCD); +} + +static void test_buffer_read32be(void) { + uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34}; + + uint32_t val = buffer_read32be(buf); + + assert(val == 0xABCD1234); +} + +static void test_buffer_read64be(void) { + uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34, + 0x56, 0x78, 0x90, 0xEF}; + + uint64_t val = buffer_read64be(buf); + + assert(val == 0xABCD1234567890EF); +} + +int main(void) { + test_buffer_write16be(); + test_buffer_write32be(); + test_buffer_write64be(); + test_buffer_read16be(); + test_buffer_read32be(); + test_buffer_read64be(); + return 0; +} From fbc86a616c38ea91beee586c417ea405ea6be56a Mon Sep 17 00:00:00 2001 From: Yu-Chen Lin Date: Sat, 30 Nov 2019 12:33:40 +0800 Subject: [PATCH 3/3] Correct coding style Signed-off-by: Yu-Chen Lin --- app/src/util/buffer_util.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/util/buffer_util.h b/app/src/util/buffer_util.h index 262df1dc..17234e42 100644 --- a/app/src/util/buffer_util.h +++ b/app/src/util/buffer_util.h @@ -36,8 +36,8 @@ buffer_read32be(const uint8_t *buf) { return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; } -static inline -uint64_t buffer_read64be(const uint8_t *buf) { +static inline uint64_t +buffer_read64be(const uint8_t *buf) { uint32_t msb = buffer_read32be(buf); uint32_t lsb = buffer_read32be(&buf[4]); return ((uint64_t) msb << 32) | lsb;