[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 08/21] tests: unit: simplify test-visitor-serialization list tests
From: |
Paolo Bonzini |
Subject: |
[PULL 08/21] tests: unit: simplify test-visitor-serialization list tests |
Date: |
Mon, 19 Sep 2022 19:34:36 +0200 |
test-visitor-serialization list tests is using an "if" to pick either the first
element of the list or the next one. This was done presumably to mimic the
code that creates the list, which has to fill in either the head pointer
or the next pointer of the last element. However, the code in the insert
phase is a pretty standard singly-linked list insertion, while the one
in the visit phase looks weird and even looks at the first item twice:
this is confusing because the test puts in 32 items and finishes with
an assertion that i == 33.
So, move the "else" step in a separate switch statement, and change
the do...while loop to a while, because cur_head has already been
initialized beforehand.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
tests/unit/test-visitor-serialization.c | 157 +++++++++++-------------
1 file changed, 69 insertions(+), 88 deletions(-)
diff --git a/tests/unit/test-visitor-serialization.c
b/tests/unit/test-visitor-serialization.c
index 907263d030..667e8fed82 100644
--- a/tests/unit/test-visitor-serialization.c
+++ b/tests/unit/test-visitor-serialization.c
@@ -427,131 +427,117 @@ static void test_primitive_lists(gconstpointer opaque)
ops->deserialize((void **)&pl_copy_ptr, serialize_data,
visit_primitive_list, &error_abort);
- i = 0;
+
+ switch (pl_copy.type) {
+ case PTYPE_STRING:
+ cur_head = pl_copy.value.strings;
+ break;
+ case PTYPE_INTEGER:
+ cur_head = pl_copy.value.integers;
+ break;
+ case PTYPE_S8:
+ cur_head = pl_copy.value.s8_integers;
+ break;
+ case PTYPE_S16:
+ cur_head = pl_copy.value.s16_integers;
+ break;
+ case PTYPE_S32:
+ cur_head = pl_copy.value.s32_integers;
+ break;
+ case PTYPE_S64:
+ cur_head = pl_copy.value.s64_integers;
+ break;
+ case PTYPE_U8:
+ cur_head = pl_copy.value.u8_integers;
+ break;
+ case PTYPE_U16:
+ cur_head = pl_copy.value.u16_integers;
+ break;
+ case PTYPE_U32:
+ cur_head = pl_copy.value.u32_integers;
+ break;
+ case PTYPE_U64:
+ cur_head = pl_copy.value.u64_integers;
+ break;
+ case PTYPE_NUMBER:
+ cur_head = pl_copy.value.numbers;
+ break;
+ case PTYPE_BOOLEAN:
+ cur_head = pl_copy.value.booleans;
+ break;
+ default:
+ g_assert_not_reached();
+ }
/* compare our deserialized list of primitives to the original */
- do {
+ i = 0;
+ while (cur_head) {
switch (pl_copy.type) {
case PTYPE_STRING: {
- strList *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.strings;
- }
+ strList *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpstr(pt->value.string, ==, ptr->value);
break;
}
case PTYPE_INTEGER: {
- intList *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.integers;
- }
+ intList *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.integer, ==, ptr->value);
break;
}
case PTYPE_S8: {
- int8List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.s8_integers;
- }
+ int8List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.s8, ==, ptr->value);
break;
}
case PTYPE_S16: {
- int16List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.s16_integers;
- }
+ int16List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.s16, ==, ptr->value);
break;
}
case PTYPE_S32: {
- int32List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.s32_integers;
- }
+ int32List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.s32, ==, ptr->value);
break;
}
case PTYPE_S64: {
- int64List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.s64_integers;
- }
+ int64List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.s64, ==, ptr->value);
break;
}
case PTYPE_U8: {
- uint8List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.u8_integers;
- }
+ uint8List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.u8, ==, ptr->value);
break;
}
case PTYPE_U16: {
- uint16List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.u16_integers;
- }
+ uint16List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.u16, ==, ptr->value);
break;
}
case PTYPE_U32: {
- uint32List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.u32_integers;
- }
+ uint32List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.u32, ==, ptr->value);
break;
}
case PTYPE_U64: {
- uint64List *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.u64_integers;
- }
+ uint64List *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(pt->value.u64, ==, ptr->value);
break;
}
case PTYPE_NUMBER: {
- numberList *ptr;
GString *double_expected = g_string_new("");
GString *double_actual = g_string_new("");
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.numbers;
- }
+ numberList *ptr = cur_head;
+ cur_head = ptr->next;
/* we serialize with %f for our reference visitors, so rather than
* fuzzy floating math to test "equality", just compare the
* formatted values
@@ -564,13 +550,8 @@ static void test_primitive_lists(gconstpointer opaque)
break;
}
case PTYPE_BOOLEAN: {
- boolList *ptr;
- if (cur_head) {
- ptr = cur_head;
- cur_head = ptr->next;
- } else {
- cur_head = ptr = pl_copy.value.booleans;
- }
+ boolList *ptr = cur_head;
+ cur_head = ptr->next;
g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
break;
}
@@ -578,9 +559,9 @@ static void test_primitive_lists(gconstpointer opaque)
g_assert_not_reached();
}
i++;
- } while (cur_head);
+ }
- g_assert_cmpint(i, ==, 33);
+ g_assert_cmpint(i, ==, 32);
ops->cleanup(serialize_data);
dealloc_helper(&pl, visit_primitive_list, &error_abort);
--
2.37.2
- [PULL 00/21] Misc patches for 2022-09-19, Paolo Bonzini, 2022/09/19
- [PULL 01/21] KVM: use store-release to mark dirty pages as harvested, Paolo Bonzini, 2022/09/19
- [PULL 02/21] target/i386: Raise #GP on unaligned m128 accesses when required., Paolo Bonzini, 2022/09/19
- [PULL 03/21] kvm: fix memory leak on failure to read stats descriptors, Paolo Bonzini, 2022/09/19
- [PULL 04/21] spapr_pci: fix leak in spapr_phb_vfio_get_loc_code, Paolo Bonzini, 2022/09/19
- [PULL 06/21] coverity: put NUBus under m68k component, Paolo Bonzini, 2022/09/19
- [PULL 07/21] smbios: sanitize type from external type before checking have_fields_bitmap, Paolo Bonzini, 2022/09/19
- [PULL 08/21] tests: unit: simplify test-visitor-serialization list tests,
Paolo Bonzini <=
- [PULL 10/21] tests: unit: add NULL-pointer check, Paolo Bonzini, 2022/09/19
- [PULL 09/21] tests: test-qga: close socket on failure to connect, Paolo Bonzini, 2022/09/19
- [PULL 05/21] coverity: add new RISC-V component, Paolo Bonzini, 2022/09/19
- [PULL 15/21] audio: add help option for -audio and -audiodev, Paolo Bonzini, 2022/09/19
- [PULL 16/21] target/i386: correctly mask SSE4a bit indices in register operands, Paolo Bonzini, 2022/09/19
- [PULL 14/21] tests/tcg: remove old SSE tests, Paolo Bonzini, 2022/09/19
- [PULL 20/21] build: remove extra parentheses causing missing rebuilds, Paolo Bonzini, 2022/09/19
- [PULL 11/21] tests/tcg: i386: fix typos in 3DNow! instructions, Paolo Bonzini, 2022/09/19
- [PULL 12/21] tests/tcg: i386: add MMX and 3DNow! tests, Paolo Bonzini, 2022/09/19
- [PULL 19/21] target/i386: introduce insn_get_addr, Paolo Bonzini, 2022/09/19