Merge pull request #967 into dev

Add some tests

<https://github.com/Genymobile/scrcpy/pull/967>
This commit is contained in:
Romain Vimont 2019-12-04 19:25:58 +01:00
commit 4041043d1c
4 changed files with 93 additions and 2 deletions

View file

@ -141,6 +141,9 @@ install_man('scrcpy.1')
### TESTS ### TESTS
tests = [ tests = [
['test_buffer_util', [
'tests/test_buffer_util.c'
]],
['test_cbuf', [ ['test_cbuf', [
'tests/test_cbuf.c', 'tests/test_cbuf.c',
]], ]],

View file

@ -36,8 +36,8 @@ buffer_read32be(const uint8_t *buf) {
return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
} }
static inline static inline uint64_t
uint64_t buffer_read64be(const uint8_t *buf) { buffer_read64be(const uint8_t *buf) {
uint32_t msb = buffer_read32be(buf); uint32_t msb = buffer_read32be(buf);
uint32_t lsb = buffer_read32be(&buf[4]); uint32_t lsb = buffer_read32be(&buf[4]);
return ((uint64_t) msb << 32) | lsb; return ((uint64_t) msb << 32) | lsb;

View file

@ -0,0 +1,76 @@
#include <assert.h>
#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;
}

View file

@ -1,5 +1,6 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <SDL2/SDL.h>
#include "util/str_util.h" #include "util/str_util.h"
@ -126,6 +127,16 @@ static void test_xstrjoin_truncated_after_sep(void) {
assert(!strcmp("abc de ", s)); 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) { static void test_utf8_truncate(void) {
const char *s = "aÉbÔc"; const char *s = "aÉbÔc";
assert(strlen(s) == 7); // É and Ô are 2 bytes-wide assert(strlen(s) == 7); // É and Ô are 2 bytes-wide
@ -166,6 +177,7 @@ int main(void) {
test_xstrjoin_truncated_in_token(); test_xstrjoin_truncated_in_token();
test_xstrjoin_truncated_before_sep(); test_xstrjoin_truncated_before_sep();
test_xstrjoin_truncated_after_sep(); test_xstrjoin_truncated_after_sep();
test_strquote();
test_utf8_truncate(); test_utf8_truncate();
return 0; return 0;
} }