diff options
| author | leiyu3 <s444814187@gmail.com> | 2022-09-22 11:41:31 -0400 |
|---|---|---|
| committer | leiyu3 <s444814187@gmail.com> | 2022-09-22 11:41:31 -0400 |
| commit | c5d89d1ba78a82e6b31aa498fb0f21c4d527e752 (patch) | |
| tree | 5ac5f694efc95a207dc06e8001003c045982f97c /vector_test.c | |
| parent | c1baa76ee589ab8158a9f1fb3173149cebe1dd1b (diff) | |
| download | vector_c-c5d89d1ba78a82e6b31aa498fb0f21c4d527e752.tar.gz vector_c-c5d89d1ba78a82e6b31aa498fb0f21c4d527e752.zip | |
write test up to remove_val
Diffstat (limited to 'vector_test.c')
| -rw-r--r-- | vector_test.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/vector_test.c b/vector_test.c new file mode 100644 index 0000000..c1f1ae4 --- /dev/null +++ b/vector_test.c | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | #include <criterion/criterion.h> | ||
| 2 | #include "vector.h" | ||
| 3 | |||
| 4 | vector_t vec; | ||
| 5 | int n = 16; | ||
| 6 | void suitesetup(void){ | ||
| 7 | vec = vector_init(n); | ||
| 8 | } | ||
| 9 | |||
| 10 | void suiteteardown(void){ | ||
| 11 | destroy_vec(&vec); | ||
| 12 | } | ||
| 13 | |||
| 14 | TestSuite(vectortests, .init=suitesetup, .fini=suiteteardown); | ||
| 15 | |||
| 16 | Test(vectortests, capacity_test){ | ||
| 17 | cr_expect(capacity(vec) == n, "Vector should have a capacity of %d", n); | ||
| 18 | cr_expect(capacity(vec) != n+1, "vector should have a capacity of %d", n); | ||
| 19 | } | ||
| 20 | |||
| 21 | Test(vectortests, is_empty_test){ | ||
| 22 | cr_assert(is_empty(vec)==1, "is_empty should return 1"); | ||
| 23 | push(&vec, 1); | ||
| 24 | cr_assert(is_empty(vec)==0, "is_empty should return 0"); | ||
| 25 | } | ||
| 26 | |||
| 27 | Test(vectortests, is_full_test){ | ||
| 28 | cr_assert(is_full(vec)==0, "is_full should return 0"); | ||
| 29 | for (int i = 0; i < n; i++){ | ||
| 30 | push(&vec, i); | ||
| 31 | } | ||
| 32 | cr_assert(is_full(vec)==1, "is_full should return 1"); | ||
| 33 | } | ||
| 34 | |||
| 35 | Test(vectortests, size_test){ | ||
| 36 | cr_assert(size(vec)==0, "Size of vector should be 0 at first"); | ||
| 37 | for (int i = 0; i < 10; i++){ | ||
| 38 | push(&vec, i); | ||
| 39 | } | ||
| 40 | cr_assert(size(vec)==10, "Size of vector should be 10"); | ||
| 41 | } | ||
| 42 | |||
| 43 | Test(vectortests, at_test){ | ||
| 44 | for (int i = 0; i < 10; i++){ | ||
| 45 | push(&vec, i+3); | ||
| 46 | } | ||
| 47 | cr_assert(at(vec, 6)==9, "at(vec, 6) should return 9"); | ||
| 48 | } | ||
| 49 | |||
| 50 | Test(vectortests, at_invalid_index_test, .exit_code = -1, .disabled=true){ | ||
| 51 | at(vec, -5); | ||
| 52 | } | ||
| 53 | |||
| 54 | Test(vectortests, push_test){ | ||
| 55 | push(&vec, 3); | ||
| 56 | push(&vec, 6); | ||
| 57 | push(&vec, 9); | ||
| 58 | |||
| 59 | cr_assert(at(vec, 1) == 6); | ||
| 60 | cr_assert(size(vec) == 3); | ||
| 61 | } | ||
| 62 | |||
| 63 | Test(vectortests, insert_test){ | ||
| 64 | push(&vec, 3); | ||
| 65 | push(&vec, 6); | ||
| 66 | push(&vec, 9); | ||
| 67 | insert(&vec, 1, 55); | ||
| 68 | |||
| 69 | cr_assert(at(vec, 1) == 55); | ||
| 70 | cr_assert(at(vec, 2) == 6); | ||
| 71 | cr_assert(at(vec, 3) == 9); | ||
| 72 | cr_assert(size(vec) == 4); | ||
| 73 | } | ||
| 74 | |||
| 75 | Test(vectortests, delete_check){ | ||
| 76 | push(&vec, 3); | ||
| 77 | push(&vec, 6); | ||
| 78 | push(&vec, 9); | ||
| 79 | insert(&vec, 1, 55); | ||
| 80 | delete_vec(&vec, 1); | ||
| 81 | |||
| 82 | cr_assert(at(vec, 1) == 6); | ||
| 83 | cr_assert(size(vec) == 3); | ||
| 84 | } | ||
| 85 | |||
| 86 | Test(vectortests, remove_test){ | ||
| 87 | for (int i = 0; i < vec.max_size; i++){ | ||
| 88 | if (i%2 == 0){ | ||
| 89 | push(&vec, 5); | ||
| 90 | continue; | ||
| 91 | } | ||
| 92 | push(&vec, i); | ||
| 93 | } | ||
| 94 | |||
| 95 | remove_val(&vec, 5); | ||
| 96 | |||
| 97 | cr_assert(size(vec) == 7); | ||
| 98 | cr_assert(at(vec, 2) == 7); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | |||
