Last active
May 20, 2016 01:48
-
-
Save mfilipelino/fccf1e5bc6cd4cad2bd835586ff55503 to your computer and use it in GitHub Desktop.
vector int
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
TODO: | |
function: | |
[] vector_erase | |
error: | |
[] implements error | |
*/ | |
#include <stdbool.h> | |
typedef struct vector{ | |
int *v; | |
int size; | |
int max_size; | |
}vector; | |
// public | |
bool vector_new(vector *vec, int size){ | |
vec->max_size = size; | |
vec->v = (int*) malloc(sizeof(int) * size); | |
vec->size = 0; | |
return true; | |
} | |
//public | |
int vector_size(vector *vec){ | |
return vec->size; | |
} | |
// public | |
int vector_max_size(vector *vec){ | |
return vec->max_size; | |
} | |
// public | |
bool vector_empty(vector *vec){ | |
return vec->size == 0 ? true : false; | |
} | |
// public | |
void vector_push_back(vector *vec, int item){ | |
if(vector_max_size(vec) > vector_size(vec)){ | |
vec->v[vec->size] = item; | |
vec->size++; | |
} | |
else{ | |
printf("Erro\n"); | |
} | |
} | |
// public | |
int vector_at(vector *vec, int i){ | |
if(!vector_empty(vec)){ | |
return vec->v[i]; | |
} | |
printf("Erro"); | |
} | |
// public | |
void vector_clear(vector *vec){ | |
if(!vector_empty(vec)){ | |
free(vec->v); | |
vec->size = 0; | |
} | |
} | |
int vector_pop_back(vector *vec){ | |
int item; | |
if(!vector_empty(vec)){ | |
item = vec->v[vec->size - 1]; | |
vec->size--; | |
} | |
return item; | |
} | |
void vector_resize(vector *vec, int size){ | |
vector_clear(vec); | |
vector_new(vec, size); | |
} | |
int* vector_interator(vector *vec){ | |
return vec->v; | |
} | |
int vector_front(vector *vec){ | |
return vec->v[0]; | |
} | |
int vector_back(vector *vec){ | |
return vec->v[vec->size - 1]; | |
} | |
// Framework unit_test c | |
vector* vec; | |
void tst_setup(char str[]){ | |
printf(str); | |
vec = (vector*) malloc(sizeof(vec)); | |
vector_new(vec, 10); | |
} | |
void tst_teardown(char str[]){ | |
free(vec->v); | |
free(vec); | |
printf(str); | |
} | |
bool tst_equal_int(int a, int b, char str[]){ | |
printf("\nfunction: %-15s | expression: %-3d == %3d | ", str, a, b); | |
if(a == b){ | |
printf("value: assert"); | |
} | |
else{ | |
printf("value: fail"); | |
} | |
printf("\n"); | |
} | |
bool tst_equal_bool(bool a, bool b, char str[]){ | |
printf("\nfunction: %-15s | expression: %s == %s | value: ", str, a ? "true" : "false", b ? "true" : "false"); | |
if(a == b){ | |
printf("assert"); | |
} | |
else{ | |
printf("fail"); | |
} | |
printf("\n"); | |
} | |
void test_step1(char str[]){ | |
tst_setup(str); | |
tst_equal_int(vec->size , 0, "vector_new"); | |
tst_teardown("\n"); | |
} | |
void test_step2(char str[]){ | |
tst_setup(str); | |
tst_equal_int(vector_size(vec), 0, "vector_size"); | |
tst_teardown("\n"); | |
} | |
void test_step3(char str[]){ | |
tst_setup(str); | |
tst_equal_int(vector_max_size(vec), 10, "vector_max_size"); | |
tst_teardown("\n"); | |
} | |
void test_step4(char str[]){ | |
tst_setup(str); | |
tst_equal_bool(vector_empty(vec), true, "vector_empty"); | |
tst_teardown("\n"); | |
} | |
void test_step5(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
tst_equal_int(vector_size(vec), 1, "vector_push_back"); | |
tst_teardown("\n"); | |
} | |
void test_step6(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
tst_equal_int(vector_size(vec), 3, "vector_push_back"); | |
tst_teardown("\n"); | |
} | |
void test_step7(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
int item = vector_at(vec, 2); | |
tst_equal_int(item, 3, "vector_at"); | |
tst_teardown("\n"); | |
} | |
void test_step8(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
vector_clear(vec); | |
tst_equal_int(vector_size(vec), 0, "vector_clear"); | |
tst_teardown("\n"); | |
} | |
void test_step9(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
int item = vector_pop_back(vec); | |
tst_equal_int(item, 3, "vector_pop_back"); | |
tst_teardown("\n"); | |
} | |
void test_step10(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
vector_pop_back(vec); | |
tst_equal_int(vector_size(vec), 2, "vector_pop_back"); | |
tst_teardown("\n"); | |
} | |
void test_step11(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
int a = vector_back(vec); | |
tst_equal_int(a, 3, "vector_back"); | |
tst_teardown("\n"); | |
} | |
void test_step12(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
int a = vector_front(vec); | |
tst_equal_int(a, 1, "vector_front"); | |
tst_teardown("\n"); | |
} | |
void test_step13(char str[]){ | |
tst_setup(str); | |
vector_push_back(vec, 1); | |
vector_push_back(vec, 2); | |
vector_push_back(vec, 3); | |
int a = vector_front(vec); | |
tst_equal_int(a, 1, "vector_front"); | |
tst_teardown("\n"); | |
} | |
void group_test_full(){ | |
test_step1("############################## STEP 1 #############################"); | |
test_step2("############################## STEP 2 #############################"); | |
test_step3("############################## STEP 3 #############################"); | |
test_step4("############################## STEP 4 #############################"); | |
test_step5("############################## STEP 5 #############################"); | |
test_step6("############################## STEP 6 #############################"); | |
test_step7("############################## STEP 7 #############################"); | |
test_step8("############################## STEP 8 #############################"); | |
test_step9("############################## STEP 9 #############################"); | |
test_step10("############################## STEP 10 #############################"); | |
test_step11("############################## STEP 11 #############################"); | |
test_step12("############################## STEP 12 #############################"); | |
} | |
int main(int argc, char *argv[]) { | |
group_test_full(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment