mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-29 05:40:27 -04:00
test: convert two spa tests to pwtest
This commit is contained in:
parent
493f0724b5
commit
7909c99ead
4 changed files with 124 additions and 93 deletions
|
|
@ -1,9 +1,7 @@
|
|||
test_apps = [
|
||||
'test-buffer',
|
||||
'test-node',
|
||||
'test-pod',
|
||||
'test-utils',
|
||||
'test-json',
|
||||
]
|
||||
|
||||
foreach a : test_apps
|
||||
|
|
|
|||
|
|
@ -51,3 +51,11 @@ test('test lib',
|
|||
include_directories: pwtest_inc,
|
||||
link_with: pwtest_lib)
|
||||
)
|
||||
|
||||
test('test spa',
|
||||
executable('test-spa',
|
||||
'test-spa-buffer.c',
|
||||
'test-spa-json.c',
|
||||
include_directories: pwtest_inc,
|
||||
link_with: pwtest_lib)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -22,57 +22,64 @@
|
|||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "pwtest.h"
|
||||
|
||||
#include <spa/buffer/alloc.h>
|
||||
#include <spa/buffer/buffer.h>
|
||||
#include <spa/buffer/meta.h>
|
||||
|
||||
static void test_abi(void)
|
||||
PWTEST(buffer_abi_types)
|
||||
{
|
||||
/* buffer */
|
||||
spa_assert(SPA_DATA_Invalid == 0);
|
||||
spa_assert(SPA_DATA_MemPtr == 1);
|
||||
spa_assert(SPA_DATA_MemFd == 2);
|
||||
spa_assert(SPA_DATA_DmaBuf == 3);
|
||||
spa_assert(SPA_DATA_MemId == 4);
|
||||
spa_assert(_SPA_DATA_LAST == 5);
|
||||
pwtest_int_eq(SPA_DATA_Invalid, 0);
|
||||
pwtest_int_eq(SPA_DATA_MemPtr, 1);
|
||||
pwtest_int_eq(SPA_DATA_MemFd, 2);
|
||||
pwtest_int_eq(SPA_DATA_DmaBuf, 3);
|
||||
pwtest_int_eq(SPA_DATA_MemId, 4);
|
||||
pwtest_int_eq(_SPA_DATA_LAST, 5);
|
||||
|
||||
/* meta */
|
||||
pwtest_int_eq(SPA_META_Invalid, 0);
|
||||
pwtest_int_eq(SPA_META_Header, 1);
|
||||
pwtest_int_eq(SPA_META_VideoCrop, 2);
|
||||
pwtest_int_eq(SPA_META_VideoDamage, 3);
|
||||
pwtest_int_eq(SPA_META_Bitmap, 4);
|
||||
pwtest_int_eq(SPA_META_Cursor, 5);
|
||||
pwtest_int_eq(SPA_META_Control, 6);
|
||||
pwtest_int_eq(SPA_META_Busy, 7);
|
||||
pwtest_int_eq(_SPA_META_LAST, 8);
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
|
||||
PWTEST(buffer_abi_sizes)
|
||||
{
|
||||
#if defined(__x86_64__) && defined(__LP64__)
|
||||
spa_assert(sizeof(struct spa_chunk) == 16);
|
||||
spa_assert(sizeof(struct spa_data) == 40);
|
||||
spa_assert(sizeof(struct spa_buffer) == 24);
|
||||
pwtest_int_eq(sizeof(struct spa_chunk), 16U);
|
||||
pwtest_int_eq(sizeof(struct spa_data), 40U);
|
||||
pwtest_int_eq(sizeof(struct spa_buffer), 24U);
|
||||
|
||||
pwtest_int_eq(sizeof(struct spa_meta), 16U);
|
||||
pwtest_int_eq(sizeof(struct spa_meta_header), 32U);
|
||||
pwtest_int_eq(sizeof(struct spa_meta_region), 16U);
|
||||
pwtest_int_eq(sizeof(struct spa_meta_bitmap), 20U);
|
||||
pwtest_int_eq(sizeof(struct spa_meta_cursor), 28U);
|
||||
|
||||
return PWTEST_PASS;
|
||||
#else
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_chunk));
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_data));
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_buffer));
|
||||
#endif
|
||||
|
||||
/* meta */
|
||||
spa_assert(SPA_META_Invalid == 0);
|
||||
spa_assert(SPA_META_Header == 1);
|
||||
spa_assert(SPA_META_VideoCrop == 2);
|
||||
spa_assert(SPA_META_VideoDamage == 3);
|
||||
spa_assert(SPA_META_Bitmap == 4);
|
||||
spa_assert(SPA_META_Cursor == 5);
|
||||
spa_assert(SPA_META_Control == 6);
|
||||
spa_assert(SPA_META_Busy == 7);
|
||||
spa_assert(_SPA_META_LAST == 8);
|
||||
|
||||
#if defined(__x86_64__) && defined(__LP64__)
|
||||
spa_assert(sizeof(struct spa_meta) == 16);
|
||||
spa_assert(sizeof(struct spa_meta_header) == 32);
|
||||
spa_assert(sizeof(struct spa_meta_region) == 16);
|
||||
spa_assert(sizeof(struct spa_meta_bitmap) == 20);
|
||||
spa_assert(sizeof(struct spa_meta_cursor) == 28);
|
||||
#else
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_meta));
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_meta_header));
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_meta_region));
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_meta_bitmap));
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_meta_cursor));
|
||||
return PWTEST_SKIP;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void test_alloc(void)
|
||||
PWTEST(buffer_alloc)
|
||||
{
|
||||
struct spa_buffer **buffers;
|
||||
struct spa_meta metas[4];
|
||||
|
|
@ -107,29 +114,34 @@ static void test_alloc(void)
|
|||
struct spa_buffer *b = buffers[i];
|
||||
fprintf(stderr, "buffer %d %p\n", i, b);
|
||||
|
||||
spa_assert(b->n_metas == SPA_N_ELEMENTS(metas));
|
||||
spa_assert(b->n_datas == SPA_N_ELEMENTS(datas));
|
||||
pwtest_int_eq(b->n_metas, SPA_N_ELEMENTS(metas));
|
||||
pwtest_int_eq(b->n_datas, SPA_N_ELEMENTS(datas));
|
||||
|
||||
for (j = 0; j < SPA_N_ELEMENTS(metas); j++) {
|
||||
spa_assert(b->metas[j].type == metas[j].type);
|
||||
spa_assert(b->metas[j].size == metas[j].size);
|
||||
pwtest_int_eq(b->metas[j].type, metas[j].type);
|
||||
pwtest_int_eq(b->metas[j].size, metas[j].size);
|
||||
fprintf(stderr, " meta %d %p\n", j, b->metas[j].data);
|
||||
spa_assert(SPA_IS_ALIGNED(b->metas[j].data, 8));
|
||||
pwtest_bool_true(SPA_IS_ALIGNED(b->metas[j].data, 8));
|
||||
}
|
||||
|
||||
for (j = 0; j < SPA_N_ELEMENTS(datas); j++) {
|
||||
spa_assert(b->datas[j].maxsize == datas[j].maxsize);
|
||||
pwtest_int_eq(b->datas[j].maxsize, datas[j].maxsize);
|
||||
fprintf(stderr, " data %d %p %p\n", j, b->datas[j].chunk, b->datas[j].data);
|
||||
spa_assert(SPA_IS_ALIGNED(b->datas[j].chunk, 8));
|
||||
spa_assert(SPA_IS_ALIGNED(b->datas[j].data, aligns[j]));
|
||||
pwtest_bool_true(SPA_IS_ALIGNED(b->datas[j].chunk, 8));
|
||||
pwtest_bool_true(SPA_IS_ALIGNED(b->datas[j].data, aligns[j]));
|
||||
}
|
||||
}
|
||||
free(buffers);
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
PWTEST_SUITE(spa_buffer)
|
||||
{
|
||||
test_abi();
|
||||
test_alloc();
|
||||
return 0;
|
||||
pwtest_add(buffer_abi_types, PWTEST_NOARG);
|
||||
pwtest_add(buffer_abi_sizes, PWTEST_NOARG);
|
||||
pwtest_add(buffer_alloc, PWTEST_NOARG);
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
|
||||
|
|
@ -22,16 +22,20 @@
|
|||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "pwtest.h"
|
||||
|
||||
#include <spa/utils/defs.h>
|
||||
#include <spa/utils/json.h>
|
||||
#include <spa/utils/string.h>
|
||||
|
||||
static void test_abi(void)
|
||||
PWTEST(json_abi)
|
||||
{
|
||||
#if defined(__x86_64__) && defined(__LP64__)
|
||||
spa_assert(sizeof(struct spa_json) == 32);
|
||||
pwtest_int_eq(sizeof(struct spa_json), 32U);
|
||||
return PWTEST_PASS;
|
||||
#else
|
||||
fprintf(stderr, "%zd\n", sizeof(struct spa_json));
|
||||
return PWTEST_SKIP;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
@ -46,22 +50,22 @@ static void test_abi(void)
|
|||
|
||||
static void check_type(int type, const char *value, int len)
|
||||
{
|
||||
spa_assert(spa_json_is_object(value, len) == (type == TYPE_OBJECT));
|
||||
spa_assert(spa_json_is_array(value, len) == (type == TYPE_ARRAY));
|
||||
spa_assert(spa_json_is_string(value, len) == (type == TYPE_STRING));
|
||||
spa_assert(spa_json_is_bool(value, len) ==
|
||||
pwtest_bool_eq(spa_json_is_object(value, len), (type == TYPE_OBJECT));
|
||||
pwtest_bool_eq(spa_json_is_array(value, len), (type == TYPE_ARRAY));
|
||||
pwtest_bool_eq(spa_json_is_string(value, len), (type == TYPE_STRING));
|
||||
pwtest_bool_eq(spa_json_is_bool(value, len),
|
||||
(type == TYPE_BOOL || type == TYPE_TRUE || type == TYPE_FALSE));
|
||||
spa_assert(spa_json_is_null(value, len) == (type == TYPE_NULL));
|
||||
spa_assert(spa_json_is_true(value, len) == (type == TYPE_TRUE || type == TYPE_BOOL));
|
||||
spa_assert(spa_json_is_false(value, len) == (type == TYPE_FALSE || type == TYPE_BOOL));
|
||||
spa_assert(spa_json_is_float(value, len) == (type == TYPE_FLOAT));
|
||||
pwtest_bool_eq(spa_json_is_null(value, len), (type == TYPE_NULL));
|
||||
pwtest_bool_eq(spa_json_is_true(value, len), (type == TYPE_TRUE || type == TYPE_BOOL));
|
||||
pwtest_bool_eq(spa_json_is_false(value, len), (type == TYPE_FALSE || type == TYPE_BOOL));
|
||||
pwtest_bool_eq(spa_json_is_float(value, len), (type == TYPE_FLOAT));
|
||||
}
|
||||
|
||||
static void expect_type(struct spa_json *it, int type)
|
||||
{
|
||||
const char *value;
|
||||
int len;
|
||||
spa_assert((len = spa_json_next(it, &value)) > 0);
|
||||
pwtest_int_gt((len = spa_json_next(it, &value)), 0);
|
||||
check_type(type, value, len);
|
||||
}
|
||||
|
||||
|
|
@ -70,24 +74,24 @@ static void expect_string(struct spa_json *it, const char *str)
|
|||
const char *value;
|
||||
int len;
|
||||
char *s;
|
||||
spa_assert((len = spa_json_next(it, &value)) > 0);
|
||||
pwtest_int_gt((len = spa_json_next(it, &value)), 0);
|
||||
check_type(TYPE_STRING, value, len);
|
||||
s = alloca(len+1);
|
||||
spa_json_parse_string(value, len, s);
|
||||
spa_assert(spa_streq(s, str));
|
||||
pwtest_str_eq(s, str);
|
||||
}
|
||||
static void expect_float(struct spa_json *it, float val)
|
||||
{
|
||||
const char *value;
|
||||
int len;
|
||||
float f;
|
||||
spa_assert((len = spa_json_next(it, &value)) > 0);
|
||||
pwtest_int_gt((len = spa_json_next(it, &value)), 0);
|
||||
check_type(TYPE_FLOAT, value, len);
|
||||
spa_assert(spa_json_parse_float(value, len, &f) > 0);
|
||||
spa_assert(f == val);
|
||||
pwtest_int_gt(spa_json_parse_float(value, len, &f), 0);
|
||||
pwtest_double_eq(f, val);
|
||||
}
|
||||
|
||||
static void test_parse(void)
|
||||
PWTEST(json_parse)
|
||||
{
|
||||
struct spa_json it[5];
|
||||
const char *json = " { "
|
||||
|
|
@ -142,32 +146,36 @@ static void test_parse(void)
|
|||
expect_string(&it[3], "empty");
|
||||
expect_type(&it[3], TYPE_ARRAY);
|
||||
spa_json_enter(&it[3], &it[4]);
|
||||
spa_assert(spa_json_next(&it[4], &value) == 0);
|
||||
pwtest_int_eq(spa_json_next(&it[4], &value), 0);
|
||||
expect_string(&it[3], "foo");
|
||||
expect_type(&it[3], TYPE_OBJECT);
|
||||
spa_json_enter(&it[3], &it[4]);
|
||||
expect_string(&it[3], "1.9");
|
||||
expect_float(&it[3], 1.9f);
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
|
||||
static void test_encode(void)
|
||||
PWTEST(json_encode)
|
||||
{
|
||||
char dst[1024];
|
||||
char dst4[4];
|
||||
char dst6[6];
|
||||
char result[1024];
|
||||
spa_assert(spa_json_encode_string(dst, sizeof(dst), "test") == 6);
|
||||
spa_assert(spa_streq(dst, "\"test\""));
|
||||
spa_assert(spa_json_encode_string(dst4, sizeof(dst4), "test") == 6);
|
||||
spa_assert(strncmp(dst4, "\"tes", 4) == 0);
|
||||
spa_assert(spa_json_encode_string(dst6, sizeof(dst6), "test") == 6);
|
||||
spa_assert(strncmp(dst6, "\"test\"", 6) == 0);
|
||||
spa_assert(spa_json_encode_string(dst, sizeof(dst), "test\"\n\r \t\b\f\'") == 20);
|
||||
spa_assert(spa_streq(dst, "\"test\\\"\\n\\r \\t\\b\\f'\""));
|
||||
spa_assert(spa_json_encode_string(dst, sizeof(dst), "\x04\x05\x1f\x20\x01\x7f\x90") == 29);
|
||||
spa_assert(spa_streq(dst, "\"\\u0004\\u0005\\u001f \\u0001\x7f\x90\""));
|
||||
spa_assert(spa_json_parse_string(dst, sizeof(dst), result) == 1);
|
||||
spa_assert(spa_streq(result, "\x04\x05\x1f\x20\x01\x7f\x90"));
|
||||
pwtest_int_eq(spa_json_encode_string(dst, sizeof(dst), "test"), 6);
|
||||
pwtest_str_eq(dst, "\"test\"");
|
||||
pwtest_int_eq(spa_json_encode_string(dst4, sizeof(dst4), "test"), 6);
|
||||
pwtest_int_eq(strncmp(dst4, "\"tes", 4), 0);
|
||||
pwtest_int_eq(spa_json_encode_string(dst6, sizeof(dst6), "test"), 6);
|
||||
pwtest_int_eq(strncmp(dst6, "\"test\"", 6), 0);
|
||||
pwtest_int_eq(spa_json_encode_string(dst, sizeof(dst), "test\"\n\r \t\b\f\'"), 20);
|
||||
pwtest_str_eq(dst, "\"test\\\"\\n\\r \\t\\b\\f'\"");
|
||||
pwtest_int_eq(spa_json_encode_string(dst, sizeof(dst), "\x04\x05\x1f\x20\x01\x7f\x90"), 29);
|
||||
pwtest_str_eq(dst, "\"\\u0004\\u0005\\u001f \\u0001\x7f\x90\"");
|
||||
pwtest_int_eq(spa_json_parse_string(dst, sizeof(dst), result), 1);
|
||||
pwtest_str_eq(result, "\x04\x05\x1f\x20\x01\x7f\x90");
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
|
||||
static void test_array(char *str, char **vals)
|
||||
|
|
@ -180,12 +188,12 @@ static void test_array(char *str, char **vals)
|
|||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], str, strlen(str));
|
||||
for (i = 0; vals[i]; i++) {
|
||||
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) > 0);
|
||||
spa_assert(spa_streq(val, vals[i]));
|
||||
pwtest_int_gt(spa_json_get_string(&it[1], val, sizeof(val)), 0);
|
||||
pwtest_str_eq(val, vals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_arrays(void)
|
||||
PWTEST(json_array)
|
||||
{
|
||||
test_array("FL,FR", (char *[]){ "FL", "FR", NULL });
|
||||
test_array(" FL , FR ", (char *[]){ "FL", "FR", NULL });
|
||||
|
|
@ -193,30 +201,35 @@ static void test_arrays(void)
|
|||
test_array("[FL FR]", (char *[]){ "FL", "FR", NULL });
|
||||
test_array("FL FR", (char *[]){ "FL", "FR", NULL });
|
||||
test_array("[ FL FR ]", (char *[]){ "FL", "FR", NULL });
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
|
||||
static void test_overflow(void)
|
||||
PWTEST(json_overflow)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
char val[3];
|
||||
const char *str = "[ F, FR, FRC ]";
|
||||
|
||||
spa_json_init(&it[0], str, strlen(str));
|
||||
spa_assert(spa_json_enter_array(&it[0], &it[1]) > 0);
|
||||
pwtest_int_gt(spa_json_enter_array(&it[0], &it[1]), 0);
|
||||
|
||||
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) > 0);
|
||||
spa_assert(spa_streq(val, "F"));
|
||||
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) > 0);
|
||||
spa_assert(spa_streq(val, "FR"));
|
||||
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) < 0);
|
||||
pwtest_int_gt(spa_json_get_string(&it[1], val, sizeof(val)), 0);
|
||||
pwtest_str_eq(val, "F");
|
||||
pwtest_int_gt(spa_json_get_string(&it[1], val, sizeof(val)), 0);
|
||||
pwtest_str_eq(val, "FR");
|
||||
pwtest_int_lt(spa_json_get_string(&it[1], val, sizeof(val)), 0);
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
PWTEST_SUITE(spa_json)
|
||||
{
|
||||
test_abi();
|
||||
test_parse();
|
||||
test_encode();
|
||||
test_arrays();
|
||||
test_overflow();
|
||||
return 0;
|
||||
pwtest_add(json_abi, PWTEST_NOARG);
|
||||
pwtest_add(json_parse, PWTEST_NOARG);
|
||||
pwtest_add(json_encode, PWTEST_NOARG);
|
||||
pwtest_add(json_array, PWTEST_NOARG);
|
||||
pwtest_add(json_overflow, PWTEST_NOARG);
|
||||
|
||||
return PWTEST_PASS;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue