aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile15
-rw-r--r--libvector.c6
-rw-r--r--main.c36
-rw-r--r--vector_test.c103
4 files changed, 115 insertions, 45 deletions
diff --git a/Makefile b/Makefile
index 14ec6f7..c251fac 100644
--- a/Makefile
+++ b/Makefile
@@ -1,17 +1,20 @@
1CC=clang 1CC=clang
2CFLAGS=-Wall -g 2CFLAGS=-Wall -g
3 3
4BINS = vector.o libvector.a main test 4BINS = vector.o libvector.a vector_test
5all: $(BINS) 5all: $(BINS)
6 6
7vector.o: vector.h libvector.c 7vector.o: vector.h libvector.c
8 $(CC) $(CFLAGS) -c libvector.c 8 $(CC) $(CFLAGS) -c libvector.c
9 9
10libvector.a: libvector.o 10libvector.a: libvector.o
11 ar -cvq libvector.a libvector.o 11 ar -cvq $@ $<
12 12
13main: main.c libvector.a 13vector_test: vector_test.c libvector.a
14 $(CC) $(CFLAGS) -o $@ $^ 14 $(CC) $(CFLAGS) -o $@ $^ -lcriterion
15 15
16test: test.c 16test:
17 gcc test.c -o test -lcriterion 17 ./vector_test
18
19clean:
20 rm *.o *.a
diff --git a/libvector.c b/libvector.c
index b113abe..60e6e42 100644
--- a/libvector.c
+++ b/libvector.c
@@ -33,9 +33,9 @@ int is_full(vector_t vec){
33} 33}
34 34
35int at(vector_t vec, int index){ 35int at(vector_t vec, int index){
36 if (index >= vec.max_size){ 36 if (index >= size(vec) || index < 0){
37 printf("Index out of bound!!\n"); 37 printf("Index out of bound!!\n");
38 exit(1); 38 exit(-1);
39 } 39 }
40 40
41 return vec.arr[index]; 41 return vec.arr[index];
@@ -46,7 +46,7 @@ void push(vector_t *vec, int value){
46 printf("Array out of Size!!\n\ 46 printf("Array out of Size!!\n\
47 Can't push!! \n\ 47 Can't push!! \n\
48 Not yet implemented resize!!\n"); 48 Not yet implemented resize!!\n");
49 exit(1); 49 exit(-1);
50 } 50 }
51 51
52 vec->arr[vec->cur_size] = value; 52 vec->arr[vec->cur_size] = value;
diff --git a/main.c b/main.c
deleted file mode 100644
index 0c0b282..0000000
--- a/main.c
+++ /dev/null
@@ -1,36 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include "vector.h"
4
5int main(void){
6 vector_t vec = vector_init(16);
7
8 printf("The size of the vector is %d.\n", size(vec));
9 printf("The capacity of the vector is %d.\n", capacity(vec));
10 printf("is_empty returns: %d.\n", is_empty(vec));
11
12
13 printf("-----\n");
14
15 for (int i = 0; i < vec.max_size; i++){
16 if (i%2 == 0){
17 push(&vec, 5);
18 continue;
19 }
20 push(&vec, i);
21 }
22 print_vec(vec);
23 remove_val(&vec, 5);
24 /* for (int i = 0; i < vec.max_size; i++){ */
25 /* printf("pop: %d\n", pop(&vec)); */
26 /* delete_vec(&vec, 0); */
27 /* } */
28
29 printf("The size of the vector is %d.\n", size(vec));
30 printf("The first element of the vector is %d.\n", at(vec, 0));
31 printf("is_empty returns: %d.\n", is_empty(vec));
32 print_vec(vec);
33
34 destroy_vec(&vec);
35 return 0;
36}
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
4vector_t vec;
5int n = 16;
6void suitesetup(void){
7 vec = vector_init(n);
8}
9
10void suiteteardown(void){
11 destroy_vec(&vec);
12}
13
14TestSuite(vectortests, .init=suitesetup, .fini=suiteteardown);
15
16Test(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
21Test(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
27Test(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
35Test(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
43Test(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
50Test(vectortests, at_invalid_index_test, .exit_code = -1, .disabled=true){
51 at(vec, -5);
52}
53
54Test(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
63Test(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
75Test(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
86Test(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