[pulseaudio-commits] 5 commits - src/tests
Arun Raghavan
arun at kemper.freedesktop.org
Fri Jul 22 09:48:37 UTC 2016
src/tests/memblockq-test.c | 260 +++++++++++++++++++++++++++++++++++++++------
1 file changed, 231 insertions(+), 29 deletions(-)
New commits:
commit ad52f9a6ba5ca7c0d76376b8c1f60ac27220ea3a
Author: Ulrich Eckhardt <ulrich.eckhardt at base-42.de>
Date: Thu May 19 21:34:22 2016 +0200
memblockq-test: Add test changing the parameters of a queue
When changing e.g. the maximum length, the target length may may have to
be adjusted, too.
diff --git a/src/tests/memblockq-test.c b/src/tests/memblockq-test.c
index 689696f..d08778e 100644
--- a/src/tests/memblockq-test.c
+++ b/src/tests/memblockq-test.c
@@ -345,6 +345,69 @@ START_TEST (memblockq_test) {
}
END_TEST
+START_TEST (memblockq_test_length_changes) {
+ pa_mempool *p;
+ pa_memblockq *bq;
+ pa_memchunk silence, data;
+ pa_sample_spec ss = {
+ .format = PA_SAMPLE_S32BE,
+ .rate = 48000,
+ .channels = 1
+ };
+ int64_t idx = 0;
+ size_t maxlength = 60;
+ size_t tlength = 40;
+ size_t prebuf = 16;
+ size_t minreq = 20;
+ size_t maxrewind = 40;
+
+ p = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0, true);
+ ck_assert_ptr_ne(p, NULL);
+
+ silence = memchunk_from_str(p, "____");
+
+ bq = pa_memblockq_new("test memblockq", idx, maxlength, tlength, &ss, prebuf, minreq, maxrewind, &silence);
+ fail_unless(bq != NULL);
+
+ data = memchunk_from_str(p, "12345678");
+
+ /* insert some data */
+ ck_assert_int_eq(pa_memblockq_push(bq, &data), 0);
+ ck_assert_int_eq(pa_memblockq_push(bq, &data), 0);
+ ck_assert_int_eq(pa_memblockq_push(bq, &data), 0);
+ ck_assert_int_eq(pa_memblockq_push(bq, &data), 0);
+
+ /* check state */
+ ck_assert_int_eq(pa_memblockq_get_length(bq), 32);
+ ck_assert_int_eq(pa_memblockq_missing(bq), 0);
+
+ /* adjust maximum length
+ * This might modify tlength, prebuf, minreq, too. */
+ pa_memblockq_set_maxlength(bq, maxlength/2);
+ check_queue_invariants(bq);
+
+ /* adjust target length
+ * This might modify minreq, too. */
+ pa_memblockq_set_tlength(bq, tlength/2);
+ check_queue_invariants(bq);
+
+ /* adjust minimum requested length
+ * This might modify prebuf, too. */
+ pa_memblockq_set_minreq(bq, minreq/2);
+ check_queue_invariants(bq);
+
+ /* adjust prebuffer length */
+ pa_memblockq_set_prebuf(bq, prebuf/2);
+ check_queue_invariants(bq);
+
+ /* cleanup */
+ pa_memblockq_free(bq);
+ pa_memblock_unref(silence.memblock);
+ pa_memblock_unref(data.memblock);
+ pa_mempool_unref(p);
+}
+END_TEST
+
START_TEST (pop_missing_test) {
int ret;
size_t missing;
@@ -438,6 +501,7 @@ int main(int argc, char *argv[]) {
tcase_add_test(tc, memchunk_from_str_test);
tcase_add_test(tc, memblockq_test_initial_properties);
tcase_add_test(tc, memblockq_test);
+ tcase_add_test(tc, memblockq_test_length_changes);
tcase_add_test(tc, pop_missing_test);
suite_add_tcase(s, tc);
commit 8299afdc9a1aea9f9cf9c8c972168b3bb3513f38
Author: Ulrich Eckhardt <ulrich.eckhardt at base-42.de>
Date: Thu May 19 22:06:12 2016 +0200
memblockq-test: Add a test that checks initial properties
Make sure that e.g. the initial size is zero and the initially missing
bytes are exactly the target length etc.
diff --git a/src/tests/memblockq-test.c b/src/tests/memblockq-test.c
index 42bf14a..689696f 100644
--- a/src/tests/memblockq-test.c
+++ b/src/tests/memblockq-test.c
@@ -191,6 +191,58 @@ START_TEST (memchunk_from_str_test) {
}
END_TEST
+START_TEST (memblockq_test_initial_properties) {
+ pa_mempool *p;
+ pa_memblockq *bq;
+ pa_memchunk silence;
+ pa_sample_spec ss = {
+ .format = PA_SAMPLE_S32BE,
+ .rate = 48000,
+ .channels = 1
+ };
+ int64_t idx = 0;
+ size_t maxlength = 100;
+ size_t tlength = 20;
+ size_t prebuf = 16;
+ size_t minreq = 8;
+ size_t maxrewind = 40;
+
+ p = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0, true);
+ ck_assert_ptr_ne(p, NULL);
+
+ silence = memchunk_from_str(p, "__");
+
+ bq = pa_memblockq_new("test memblockq", idx, maxlength, tlength, &ss, prebuf, minreq, maxrewind, &silence);
+ fail_unless(bq != NULL);
+
+ /* check initial properties */
+ ck_assert_int_eq(pa_memblockq_is_readable(bq), false);
+ ck_assert_int_eq(pa_memblockq_get_length(bq), 0);
+ ck_assert_int_eq(pa_memblockq_missing(bq), tlength);
+ ck_assert_int_eq(pa_memblockq_get_maxlength(bq), maxlength);
+ ck_assert_int_eq(pa_memblockq_get_tlength(bq), tlength);
+ ck_assert_int_eq(pa_memblockq_get_prebuf(bq), prebuf);
+ ck_assert_int_eq(pa_memblockq_get_minreq(bq), minreq);
+ ck_assert_int_eq(pa_memblockq_get_maxrewind(bq), maxrewind);
+ ck_assert_int_eq(pa_memblockq_get_base(bq), pa_frame_size(&ss));
+ ck_assert_int_eq(pa_memblockq_get_read_index(bq), 0);
+ ck_assert_int_eq(pa_memblockq_get_write_index(bq), 0);
+
+ check_queue_invariants(bq);
+
+ /* Check reporting of missing bytes:
+ * Initially, tlength bytes are missing. The second call doesn't
+ * report additional missing data since the first call. */
+ ck_assert_int_eq(pa_memblockq_pop_missing(bq), tlength);
+ ck_assert_int_eq(pa_memblockq_pop_missing(bq), 0);
+
+ /* cleanup */
+ pa_memblockq_free(bq);
+ pa_memblock_unref(silence.memblock);
+ pa_mempool_unref(p);
+}
+END_TEST
+
START_TEST (memblockq_test) {
int ret;
@@ -384,6 +436,7 @@ int main(int argc, char *argv[]) {
s = suite_create("Memblock Queue");
tc = tcase_create("memblockq");
tcase_add_test(tc, memchunk_from_str_test);
+ tcase_add_test(tc, memblockq_test_initial_properties);
tcase_add_test(tc, memblockq_test);
tcase_add_test(tc, pop_missing_test);
suite_add_tcase(s, tc);
commit a295cb87cf62f787fe65e40b5e6499ff531a4aaa
Author: Ulrich Eckhardt <ulrich.eckhardt at base-42.de>
Date: Thu May 19 20:00:46 2016 +0200
memblockq-test: Some refactoring
- Set the loglevel once in the main entry code instead of in each test function.
- Check pool allocation succeeded.
- Reduce code by using utility function to allocate chunks.
- Improve coverage by using utility function to validate queue invariants.
diff --git a/src/tests/memblockq-test.c b/src/tests/memblockq-test.c
index b6d4f17..42bf14a 100644
--- a/src/tests/memblockq-test.c
+++ b/src/tests/memblockq-test.c
@@ -204,42 +204,21 @@ START_TEST (memblockq_test) {
.channels = 1
};
- pa_log_set_level(PA_LOG_DEBUG);
-
p = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0, true);
+ ck_assert_ptr_ne(p, NULL);
- silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1);
- fail_unless(silence.memblock != NULL);
-
- silence.index = 0;
- silence.length = pa_memblock_get_length(silence.memblock);
+ silence = memchunk_from_str(p, "__");
bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence);
fail_unless(bq != NULL);
+ check_queue_invariants(bq);
- chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1);
- fail_unless(chunk1.memblock != NULL);
-
- chunk1.index = 0;
- chunk1.length = 2;
-
- chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1);
- fail_unless(chunk2.memblock != NULL);
-
- chunk2.index = 2;
- chunk2.length = 2;
-
- chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1);
- fail_unless(chunk3.memblock != NULL);
-
- chunk3.index = 0;
- chunk3.length = 4;
-
- chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1);
- fail_unless(chunk4.memblock != NULL);
-
- chunk4.index = 0;
- chunk4.length = 8;
+ chunk1 = memchunk_from_str(p, "11");
+ chunk2 = memchunk_from_str(p, "XX22");
+ chunk2.index += 2;
+ chunk2.length -= 2;
+ chunk3 = memchunk_from_str(p, "3333");
+ chunk4 = memchunk_from_str(p, "44444444");
ret = pa_memblockq_push(bq, &chunk1);
fail_unless(ret == 0);
@@ -253,6 +232,8 @@ START_TEST (memblockq_test) {
ret = pa_memblockq_push(bq, &chunk4);
fail_unless(ret == 0);
+ check_queue_invariants(bq);
+
pa_memblockq_seek(bq, -6, 0, true);
ret = pa_memblockq_push(bq, &chunk3);
fail_unless(ret == 0);
@@ -299,6 +280,8 @@ START_TEST (memblockq_test) {
dump(bq, 1);
+ check_queue_invariants(bq);
+
pa_memblockq_free(bq);
pa_memblock_unref(silence.memblock);
pa_memblock_unref(chunk1.memblock);
@@ -395,6 +378,9 @@ int main(int argc, char *argv[]) {
TCase *tc;
SRunner *sr;
+ if (!getenv("MAKE_CHECK"))
+ pa_log_set_level(PA_LOG_DEBUG);
+
s = suite_create("Memblock Queue");
tc = tcase_create("memblockq");
tcase_add_test(tc, memchunk_from_str_test);
commit 37b0ce03338ddd17a31064b7ff16a517817d216a
Author: Ulrich Eckhardt <ulrich.eckhardt at base-42.de>
Date: Sat May 28 11:15:49 2016 +0200
memblockq-test: Utility function to validate queue invariants
In particular, the relations between base, minreq, tlength, length,
missing, maxlength follow certain rules. On change, these invariants can
be violated, which requires additional code to restore them. Setting one
value can thus cause a cascade of changes. This utility function can
assert those invariants after changing something.
diff --git a/src/tests/memblockq-test.c b/src/tests/memblockq-test.c
index 969e512..b6d4f17 100644
--- a/src/tests/memblockq-test.c
+++ b/src/tests/memblockq-test.c
@@ -110,6 +110,68 @@ static void dump(pa_memblockq *bq, int n) {
fprintf(stderr, "<\n");
}
+/*
+ * utility function to validate invariants
+ *
+ * The different values like base, maxlength etc follow certain rules.
+ * This convenience function makes sure that changes don't violate
+ * these rules.
+ */
+static void check_queue_invariants(pa_memblockq *bq) {
+ size_t base = pa_memblockq_get_base(bq);
+ size_t maxlength = pa_memblockq_get_maxlength(bq);
+ size_t tlength = pa_memblockq_get_tlength(bq);
+ size_t minreq = pa_memblockq_get_minreq(bq);
+ size_t prebuf = pa_memblockq_get_prebuf(bq);
+ size_t length = pa_memblockq_get_length(bq);
+ size_t missing = pa_memblockq_missing(bq);
+
+ /* base > zero */
+ ck_assert_int_gt(base, 0);
+
+ /* maxlength multiple of base
+ * maxlength >= base */
+ ck_assert_int_eq(maxlength % base, 0);
+ ck_assert_int_ge(maxlength, base);
+
+ /* tlength multiple of base
+ * tlength >= base
+ * tlength <= maxlength */
+ ck_assert_int_eq(tlength % base, 0);
+ ck_assert_int_ge(tlength, base);
+ ck_assert_int_le(tlength, maxlength);
+
+ /* minreq multiple of base
+ * minreq >= base
+ * minreq <= tlength */
+ ck_assert_int_eq(minreq % base, 0);
+ ck_assert_int_ge(minreq, base);
+ ck_assert_int_le(minreq, tlength);
+
+ /* prebuf multiple of base
+ * prebuf >= 0
+ * prebuf <= tlength + base - minreq
+ * prebuf <= tlength (because minreq >= base) */
+ ck_assert_int_eq(prebuf % base, 0);
+ ck_assert_int_ge(prebuf, 0);
+ ck_assert_int_le(prebuf, tlength + base - minreq);
+ ck_assert_int_le(prebuf, tlength);
+
+ /* length >= 0
+ * length <= maxlength */
+ ck_assert_int_ge(length, 0);
+ ck_assert_int_le(length, maxlength);
+
+ /* missing >= 0
+ * missing <= tlength
+ * minimum reported amount of missing data is minreq
+ * reported amount of missing data is target length minus actual length */
+ ck_assert_int_ge(missing, 0);
+ ck_assert_int_le(missing, tlength);
+ ck_assert((missing == 0) || (missing >= minreq));
+ ck_assert((missing == 0) || (missing == tlength - length));
+}
+
START_TEST (memchunk_from_str_test) {
pa_mempool *p;
pa_memchunk chunk;
commit ac72e85f0a0aade2d1c62aaf356b7039c4387139
Author: Ulrich Eckhardt <ulrich.eckhardt at base-42.de>
Date: Thu May 19 21:32:22 2016 +0200
memblockq-test: Utility function to alloc chunks
This eases creating a chunk from a string for writing tests.
diff --git a/src/tests/memblockq-test.c b/src/tests/memblockq-test.c
index c14e394..969e512 100644
--- a/src/tests/memblockq-test.c
+++ b/src/tests/memblockq-test.c
@@ -42,6 +42,23 @@ static const char *manual[] = {
"__________________3333______________________________"
};
+/*
+ * utility function to create a memchunk
+ */
+static pa_memchunk memchunk_from_str(pa_mempool *p, const char* data)
+{
+ pa_memchunk res;
+ size_t size = strlen(data);
+
+ res.memblock = pa_memblock_new_fixed(p, (void*)data, size, true);
+ ck_assert_ptr_ne(res.memblock, NULL);
+
+ res.index = 0;
+ res.length = pa_memblock_get_length(res.memblock);
+
+ return res;
+}
+
static void dump_chunk(const pa_memchunk *chunk, pa_strbuf *buf) {
size_t n;
void *q;
@@ -93,6 +110,25 @@ static void dump(pa_memblockq *bq, int n) {
fprintf(stderr, "<\n");
}
+START_TEST (memchunk_from_str_test) {
+ pa_mempool *p;
+ pa_memchunk chunk;
+
+ p = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0, true);
+ ck_assert_ptr_ne(p, NULL);
+
+ /* allocate memchunk and check default settings */
+ chunk = memchunk_from_str(p, "abcd");
+ ck_assert_ptr_ne(chunk.memblock, NULL);
+ ck_assert_int_eq(chunk.index, 0);
+ ck_assert_int_eq(chunk.length, 4);
+
+ /* cleanup */
+ pa_memblock_unref(chunk.memblock);
+ pa_mempool_unref(p);
+}
+END_TEST
+
START_TEST (memblockq_test) {
int ret;
@@ -299,6 +335,7 @@ int main(int argc, char *argv[]) {
s = suite_create("Memblock Queue");
tc = tcase_create("memblockq");
+ tcase_add_test(tc, memchunk_from_str_test);
tcase_add_test(tc, memblockq_test);
tcase_add_test(tc, pop_missing_test);
suite_add_tcase(s, tc);
More information about the pulseaudio-commits
mailing list