int main(){ ElemType userVal; printf("Please enter a set of integers.\n"); int checker; LIST* lst = lst_create(); while (1){ checker = scanf("%i", &userVal); if (checker == EOF || checker == 0) break; lst_push_back(lst, userVal); } printf("The list before the quick sort: \n"); lst_print(lst); printf("The list after the quick sort: \n"); qsort1(lst); lst_print(lst); lst_free(lst); return 0; }
void *parse_light(const parsing_sect_t *section, t_scene *scene) { t_light *light; t_vec3 position; t_vec3 color; float intensity; vec3_set(&position, 0.f, 0.f, 0.f); if (section->option_count > 0) parse_vec3(section->options[0] + 1, &position); vec3_set(&color, 1.f, 1.f, 1.f); if (section->option_count > 1) parse_vec3(section->options[1] + 1, &color); intensity = 0.f; if (section->option_count > 2) intensity = atof(section->options[2][1]); light = create_light(&position, intensity, &color); lst_push_back(scene->lights, light); return (light); }
t_obj *get_obj_from_file(char *file) { int fd; char *buf; t_obj *lst; t_obj *bgn_lst; lst = NULL; bgn_lst = NULL; if ((fd = check_file(file)) == -1) file_error("Can't open file : ", file); while (get_next_line(fd, &buf) > 0) { if (buf[0] == '/' || buf[0] == '*') ; else if (buf[0] == '&') lst = build_lnk(buf); else if (buf[0] == '-' || (buf[0] >= '0' && buf[0] <= '9')) { lst = fill_data(buf, lst); bgn_lst = lst_push_back(lst, bgn_lst); } free(buf); buf = NULL; } return (bgn_lst); }
void *parse_sphere(const parsing_sect_t *section, t_scene *scene) { t_sphere *sphere; t_object *object; t_vec3 position; t_material *material; float radius; vec3_set(&position, 0, 0, 0); if (section->option_count > 0) parse_vec3(section->options[0] + 1, &position); radius = 0.f; if (section->option_count > 1) radius = atof(section->options[1][1]); material = NULL; if (section->option_count > 2) material = get_material(scene, section->options[2][1]); if (material == NULL) die("Unknown material."); sphere = create_sphere(&position, radius, material); object = create_object(section->name, SPHERE, sphere); lst_push_back(scene->objects, object); return (sphere); }
// add an item to the list, make a new link, push it back t_link *lst_add(t_lst *lst,void *data,const char *name) { t_link *link=link_new(name); link->data=data; lst_push_back(lst,link); return link; }
int parse_normal(const char **tokens) { t_vec3 *new_normal; if (!(new_normal = malloc(sizeof(t_vec3)))) return (0); else if (!parse_vec3(tokens, new_normal)) { free(new_normal); parser_die("A normal needs three arguments."); } lst_push_back(g_current_data->normals, new_normal); return (1); }
int parse_color(const char **tokens) { t_vec2 *new_color; new_color = NULL; if (!tokens_are_enough(tokens, 2)) parser_die("A texture needs two arguments."); else if (!(new_color = malloc(sizeof(t_vec2)))) return (0); new_color->x = atof(tokens[0]); new_color->y = atof(tokens[1]); lst_push_back(g_current_data->uvs, new_color); return (1); }
int add_vertex(const char **tokens, int no_texture, t_lst *vertices) { t_vertex *new_vertex; new_vertex = NULL; if (!tokens[0]) parser_die("A face component can't be empty."); else if (!(new_vertex = malloc(sizeof(t_vertex)))) return (0); fill_vertex_position(tokens, new_vertex); fill_vertex_color(tokens, new_vertex, no_texture); fill_vertex_normal(tokens, new_vertex, no_texture); lst_push_back(vertices, new_vertex); return (1); }
int parse_polygon(const char **tokens) { t_polygon *new_polygon; if (!(new_polygon = malloc(sizeof(t_polygon)))) return (0); new_polygon->vertices = new_lst(); if (!parse_polygon_components(tokens, new_polygon)) { free(new_polygon); return (0); } lst_push_back(g_current_data->polygons, new_polygon); return (1); }
int main() { // Test values. LIST *empty = lst_create(); LIST *anotherEmptyList = lst_create(); LIST *one = lst_create(); // List of one element [ 5 ]. LIST *medium = lst_create(); // List of ten elements [ 1 2 ... 9 10 ]. LIST *evenList; // Lst of five even elements [ 2 4 6 8 10 ]. LIST *oddList; // List of five odd elements [ 1 3 5 7 9 ]. LIST *dupsOfevenList; // List of some even duplicate elements [ 4 8 12 16 20 ]. LIST *resultList; srand(time(NULL)); lst_push_back(one, 5); for(int i = 1; i <= 10; ++i) { lst_push_back(medium, i); } /** * Unit test for void lst_push_back(LIST *l, ElemType val); */ printf("\n\nTest: 0A\nDescription: Testing lst_push_back with List of length 0 with elements [] and val=42\nExpected output: [ 42 ]\n"); printf("Your output:"); lst_push_back(empty, 42); lst_print(empty); FREE(empty); empty = lst_create(); printf("\n\nTest: 0B\nDescription: Testing lst_push_back with List of length 1 with elements [ 5 ] and val=42\nExpected output: [ 5 42 ]\n"); printf("Your output:"); lst_push_back(one, 42); lst_print(one); FREE(one); one = lst_create(); lst_push_back(one, 5); // Reset one for later tests. evenList = lst_create(); for (int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); } printf("\n\nTest: 0C\nDescription: Testing lst_push_back with List of length 5 with elements [2 4 6 8 10] and val=42\nExpected output: [ 2 4 6 8 10 42 ]\n"); printf("Your output:"); lst_push_back(evenList, 42); lst_print(evenList); FREE(evenList); /** * 2. Unit tests on function: int lst_count(LIST *l, ElemType x); */ printf("\n\nTest: 2A\nDescription: Testing lst_count with List of length 0 and x=5\nExpected output: 0\n"); printf("Your output: %d\n", lst_count(empty, 5)); printf("\n\nTest: 2B\nDescription: Testing lst_count with List of length 10 and x=-5\nExpected output: 0\n"); printf("Your output: %d\n", lst_count(medium, -5)); printf("\n\nTest: 2C\nDescription: Testing lst_count with List of length 10 and x=5\nExpected output: 1\n"); printf("Your output: %d\n", lst_count(medium, 5)); lst_push_front(medium, 10); printf("\n\nTest: 2D\nDescription: Testing lst_count with List of length 10 and x=10\nExpected output: 2\n"); printf("Your output: %d\n", lst_count(medium, 10)); lst_pop_front(medium); // Reset value for later tests. /** * 4. Unit tests on function: void lst_reverse(LIST *l); */ printf("\n\nTest: 4A\nDescription: Testing lst_reverse with List of length 0\nExpected output: []\n"); printf("Your output: "); lst_reverse(empty); lst_print(empty); printf("\n\nTest: 4B\nDescription: Testing lst_reverse with List of length 1\nExpected output: [ 5 ]\n"); printf("Your output: "); lst_reverse(one); lst_print(one); printf("\n\nTest: 4C\nDescription: Testing lst_reverse with List of length 10\nExpected output: [ 10 9 8 7 6 5 4 3 2 1 ]\n"); printf("Your output: "); lst_reverse(medium); lst_push_back(medium, 99); lst_pop_back(medium); // Check for tail correctness. lst_print(medium); FREE(medium); medium = lst_create(); for (int i=1; i<=10; ++i) { lst_push_back(medium, i); } // Reset value for later tests. /** * 6. Unit tests on void lst_insert_sorted(LIST *l, ElemType x); */ evenList = lst_create(); for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); } printf("\n\nTest: 6A\nDescription: Testing lst_insert_sorted with List of length 0 and x=5\nExpected output: [ 5 ]\n"); printf("Your output: "); lst_insert_sorted(empty, 5); lst_print(empty); FREE(empty); empty = lst_create(); // Resets value for later tests. printf("\n\nTest: 6B\nDescription: Testing lst_insert_sorted with List of length 5 and x=5\nExpected output: [ 2 4 5 6 8 10 ]\n"); printf("Your output: "); lst_insert_sorted(evenList, 5); lst_print(evenList); FREE(evenList); evenList = lst_create(); for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); } printf("\n\nTest: 6C\nDescription: Testing lst_insert_sorted with List of length 5 and x=0\nExpected output: [ 0 2 4 6 8 10 ]\n"); printf("Your output: " ); lst_insert_sorted(evenList, 0); lst_print(evenList); FREE(evenList); evenList = lst_create(); for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); } printf("\n\nTest: 6D\nDescription: Testing lst_insert_sorted with List of length 5 and x=12\nExpected output: [ 2 4 6 8 10 12 ]\n"); printf("Your output: "); lst_insert_sorted(evenList, 12); lst_print(evenList); FREE(evenList); evenList = lst_create(); for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); } // Reset value for later tests. /** * 8. Unit tests on void lst_merge_sorted(LIST *a, LIST *b); */ printf("\n\nTest: 8A\nDescription: Testing lst_merge_sorted with List a of length 0 with elements [] and List b of length 0 with elements []\n"); printf("Your output:"); lst_merge_sorted(anotherEmptyList, empty); lst_print(anotherEmptyList); FREE(anotherEmptyList); FREE(empty); empty = lst_create(); anotherEmptyList = lst_create(); printf("\n\nTest: 8B\nDescription: Testing lst_merge_sorted with List a of length 1 with elements [ 5 ] and List b of length 0 with elements []\n"); printf("\nExpected output: [ 5 ]\n"); printf("Your output: "); lst_merge_sorted(one, empty); lst_print(one); FREE(one); FREE(empty); one = lst_create(); empty = lst_create(); lst_push_back(one, 10); printf("\n\nTest: 8C\nDescription: Testing lst_merge_sorted with List a of length 0 with elements [] and List b of length 1 with elements [ 10 ]\n"); printf("\nExpected output: [ 10 ]\n"); printf("Your output: "); lst_merge_sorted(empty, one); lst_print(empty); FREE(empty); FREE(one); empty = lst_create(); one = lst_create(); lst_push_back(one, 5); oddList = lst_create(); for(int i=0; i<5; ++i) { lst_push_back(oddList, i*2+1); } printf("\n\nTest: 8D\nDescription: Testing lst_merge_sorted with List a of length 5 with elements [ 2 4 6 8 10 ] and List b of length 5 with elements [ 1 3 5 7 9 ]"); printf("\nExpected output: [ 1 2 3 4 5 6 7 8 9 10 ]\n"); printf("Your output: "); lst_merge_sorted(evenList, oddList); lst_print(evenList); FREE(evenList); FREE(oddList); evenList = lst_create(); dupsOfevenList = lst_create(); for (int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); lst_push_back(dupsOfevenList, i*4); } printf("\n\nTest: 8E\nDescription: Testing lst_merge_sorted with List a of length 5 with elements [ 2 4 6 8 10 ] and List b of length 5 with elements [ 4 8 12 16 20 ]\n"); printf("\nExpected output: [ 2 4 4 6 8 8 10 12 16 20 ]\n"); printf("Your output: "); lst_merge_sorted(evenList, dupsOfevenList); lst_print(evenList); FREE(evenList); FREE(dupsOfevenList); /** * 10. Unit tests on LIST * lst_from_array(ElemType a[], int n); **/ int emptyArray[] = {}; int oneArray[] = {5}; int manyArray[] = {1,2,3,4,5}; printf("\n\nTest: 10A\nDescription: Testing lst_from_array with array of length 0 with elements [] and n=0\nExpected output: []\n"); printf("Your output:"); resultList = lst_from_array(emptyArray, 0); if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); } if (resultList) { FREE(resultList);} printf("\n\nTest: 10B\nDescription: Testing lst_from_array with Array of length 1 with elements [ 5 ] and n=1\nExpected output: [ 5 ]\n"); printf("Your output:"); resultList = lst_from_array(oneArray, 1); if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); } if (resultList) { FREE(resultList); } printf("\n\nTest: 10C\nDescription: Testing lst_from_array with Array of length 5 with elements [ 1 2 3 4 5 ] and n=5\nExpected output: [ 1 2 3 4 5 ]\n"); printf("Your output:"); resultList = lst_from_array(manyArray, 5); if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); } if (resultList) { FREE(resultList); } /** * 12. Unit tests for LIST * lst_prefix(LIST *lst, unsigned int k); */ printf("\n\nTest: 12A\nDescription: Testing lst_prefix with List of length 0 with elements [] and k=0\nExpected output: []\n"); printf("Expected return value: []"); printf("Your output:\n"); resultList = lst_prefix(empty, 0); lst_print(empty); printf("Your return value: "); if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); } if (resultList) { FREE(resultList); } FREE(empty); empty = lst_create(); printf("\n\nTest: 12B\nDescription: Testing lst_prefix with List of length 0 with elements [] and k=3\nExpected output: []\n"); printf("Expected return value: []\n"); printf("Your output:"); resultList = lst_prefix(empty, 3); lst_print(empty); printf("Your return value: "); if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); } if (resultList) { FREE(resultList); } FREE(empty); empty = lst_create(); printf("\n\nTest: 12C\nDescription: Testing lst_prefix with List of length 1 with elements [ 5 ] and k=0\nExpected output: [ 5 ]\n"); printf("Expected return value: []\n"); printf("Your output:"); resultList = lst_prefix(one, 0); lst_print(one); printf("Your return value: "); if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); } if (resultList) { FREE(resultList); } FREE(one); one = lst_create(); lst_push_back(one, 5); evenList = lst_create(); for(int i=1; i<=5; i++) { lst_push_back(evenList, i*2); } printf("\n\nTest: 12D\nDescription: Testing lst_prefix with List of length 5 with elements [ 2 4 6 8 10 ] and k=3\nExpected output: [ 8 10 ]\n"); printf("Expected return value: [ 2 4 6 ]\n"); printf("Your output: "); resultList = lst_prefix(evenList, 3); lst_print(evenList); printf("Your return value: "); if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); } if (resultList) { FREE(resultList); } //FREE(evenList); /** * 14. Unit test for void lst_concat(LIST *a, LIST *b); */ anotherEmptyList = lst_create(); evenList = lst_create(); oddList = lst_create(); for(int i=1; i<=5; i++) { lst_push_back(evenList, i*2); lst_push_back(oddList, i*2-1); } // 2 4 6 8 10 printf("\n\nTest: 14A\nDescription: Testing lst_concat with List a of length 0 with elements [] and List b of length 0 with elements []\nExpected output: []\n"); printf("Your output:"); lst_concat(anotherEmptyList, empty); lst_print(anotherEmptyList); FREE(anotherEmptyList); FREE(empty); empty = lst_create(); anotherEmptyList = lst_create(); printf("\n\nTest: 14B\nDescription: Testing lst_concat with List a of length 1 with elements [ 5 ] and List b of length 0 with elements []\nExpected output: [ 5 ]\n"); printf("Your output:"); lst_concat(one, anotherEmptyList); lst_print(one); FREE(one); FREE(anotherEmptyList); anotherEmptyList = lst_create(); one = lst_create(); lst_push_back(one, 10); printf("\n\nTest: 14C\nDescription: Testing lst_concat with List a of length 0 with elements []"); printf(" and List b of length 1 with elements [ 10 ]\nExpected output: [ 10 ]\n"); printf("Your output:"); lst_concat(anotherEmptyList, one); lst_print(anotherEmptyList); FREE(anotherEmptyList); FREE(one); one = lst_create(); lst_push_back(one, 5); printf("\n\nTest: 14D\nDescription: Testing lst_concat with List a of length 5 with elements [ 2 4 6 8 10]"); printf(" and List b of length 5 with elements [ 1 3 5 7 9 ]\nExpected output: [ 2 4 6 8 10 1 3 5 7 9 ]\n"); printf("Your output:"); lst_concat(evenList, oddList); lst_print(evenList); FREE(evenList); FREE(oddList); evenList = lst_create(); dupsOfevenList = lst_create(); for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); lst_push_back(dupsOfevenList, i*4); } printf("\n\nTest: 14E\nDescription: Testing lst_concat with List a of length 5 with elements [ 2 4 6 8 10 ] and List b of length 5 with elements [ 4 8 12 16 20 ]\n" "Expected output: [ 2 4 6 8 10 4 8 12 16 20 ]\n"); printf("Your output:"); lst_concat(evenList, dupsOfevenList); lst_print(evenList); FREE(evenList); FREE(dupsOfevenList); }
int main() { printf("REMINDER: This test suite is NOT exhaustive. Passing all of these tests does NOT mean you will get 100%% on the project.\n"); srand(time(NULL)); int i; /********* 0 lst_is_empty **********/ LIST *empty = lst_create(); LIST *one = lst_create(); LIST *large = lst_create(); lst_push_back(one, 5); int rand_size = rand() % 20; for(i = 0; i < rand_size; i++) { lst_push_back(large, i); } printf("\n\nTest: 0A\nDescription: Testing lst_is_empty with List of length 0\nExpected output: 1\n"); printf("Your output: %d\n", lst_is_empty(empty)); printf("\n\nTest: 0B\nDescription: Testing lst_is_empty with List of length 1\nExpected output: 0\n"); printf("Your output: %d\n", lst_is_empty(one)); printf("\n\nTest: 0CnDescription: Testing lst_is_empty with List of length %d\nExpected output: %d\n", rand_size, ((rand_size == 0) ? 1 : 0)); printf("Your output: %d\n", lst_is_empty(large)); FREE(large); FREE(one); FREE(empty); /********* 1 lst_print_rev **********/ LIST *rev1 = lst_create(); LIST *rev2 = lst_create(); for(i = 0; i < 5; i++) { lst_push_back(rev2, i); } printf("\n\nTest: 1A\nDescription: Testing lst_print_rev with List of length 5\nExpected output: \n[4, 3, 2, 1, 0]\n"); printf("Your output\n"); lst_print_rev(rev2); printf("\n\nTest: 1B\nDescription: Testing lst_print_rev with List of length 0\nExpected output: \n[ ]\n"); printf("Your output\n"); lst_print_rev(rev1); FREE(rev1); FREE(rev2); /********** 3 lst_pop_back ***********/ large = lst_create(); one = lst_create(); empty = lst_create(); for(i = 0; i < 8; i++) { lst_push_back(large, i); } lst_push_back(one, 6); printf("\n\nTest: 3A\nDescription: Testing lst_pop_back with List of length 0\nExpected output: <some arbitrary number (NO SEG FAULT!)>\n"); printf("Your output: %d\n", lst_pop_back(empty)); printf("\n\nTest: 3B\nDescription: Testing lst_pop_back with List of length 1\nExpected output: 6\n"); printf("Your output: %d\n", lst_pop_back(one)); printf("\n\nTest: 3C\nDescription: Testing lst_pop_back with List of length 8\nExpected output: 7\n"); printf("Your output: %d\n", lst_pop_back(large)); FREE(large); FREE(one); FREE(empty); /********** 5 lst_is_sorted ***********/ empty = lst_create(); LIST *sorted = lst_create(); LIST *unsorted = lst_create(); for(i = 0; i < 10; i++) { lst_push_back(sorted, i); lst_push_back(unsorted, i); } lst_push_back(unsorted, 5); printf("\n\nTest: 5A\nDescription: Testing lst_is_sorted with List of length 0\nExpected output: 1\n"); printf("Your output: %d\n", lst_is_sorted(empty)); printf("\n\nTest: 5B\nDescription: Testing lst_is_sorted with List of length 11\nExpected output: 0\n"); printf("Your output: %d\n", lst_is_sorted(unsorted)); printf("\n\nTest: 5C\nDescription: Testing lst_is_sorted with List of length 10\nExpected output: 1\n"); printf("Your output: %d\n", lst_is_sorted(sorted)); FREE(empty); FREE(sorted); FREE(unsorted); /********** 7 lst_length ***********/ empty = lst_create(); rand_size = rand() % 100; LIST *small = lst_create(); large = lst_create(); for(i = 0; i < rand_size; i++) { lst_push_back(large, rand() % 1000); } lst_push_back(small, 3); printf("\n\nTest: 7A\nDescription: Testing lst_length with List of length 0\nExpected output: 0\n"); printf("Your output: %d\n", lst_length(empty)); printf("\n\nTest: 7B\nDescription: Testing lst_length with List of length 1\nExpected output: 1\n"); printf("Your output: %d\n", lst_length(small)); printf("\n\nTest: 7C\nDescription: Testing lst_length with List of length %d\nExpected output: %d\n", rand_size, rand_size); printf("Your output: %d\n", lst_length(large)); FREE(empty); FREE(small); FREE(large); /********** 9 lst_clone ***********/ empty = lst_create(); small = lst_create(); large = lst_create(); lst_push_back(small, 3); rand_size = rand() % 10; for(i = 0; i < rand_size; i++) { lst_push_back(large, rand() % 500); } printf("\n\nTest 9A\nDescription: Testing lst_clone with List of length 0\nExpected output:\n"); lst_print(empty); printf("Your output: \n"); LIST *empty_clone = lst_clone(empty); lst_print(empty_clone); printf("\n\nTest 9B\nDescription: Testing lst_clone with List of length 1\nExpected output:\n"); lst_print(small); printf("Your output: \n"); LIST *small_clone = lst_clone(small); lst_print(small_clone); printf("\n\nTest 9C\nDescription: Testing lst_clone with List of random size\nExpected output:\n"); lst_print(large); printf("Your output: \n"); LIST *large_clone = lst_clone(large); lst_print(large_clone); FREE(empty); FREE(small); FREE(large); /********** 11 lst_to_array ***********/ large = lst_create(); rand_size = rand() % 50; for(i = 0; i < rand_size; i++) { lst_push_back(large, rand() % 100); } printf("\n\nTest 11A\nDescription: Testing lst_to_array with List of length %d\nExpected output:\n", rand_size); lst_print(large); printf("Your output: \n"); int *arr = lst_to_array(large); print_arr(arr, lst_length(large)); FREE(large); /********** 13 lst_filter_leq ***********/ large = lst_create(); empty = lst_create(); small = lst_create(); lst_push_back(small, 7); for(i = 0; i < 10; i++) { lst_push_back(large, i); } printf("\n\nTest 13A\nDescription: Testing lst_filter_leq with List of length 0 and cutoff of 5\nExpected output for original List after call to lst_filter_leq:\n"); LIST *leq_empty = lst_filter_leq(empty, 5); printf("[ ]\n"); printf("Your output of original List:\n"); lst_print(empty); printf("Expected output for new list of leq elements:\n"); printf("[ ]\n"); printf("Your output of new list of leq elements:\n"); lst_print(leq_empty); printf("\n\nTest 13B\nDescription: Testing lst_filter_leq with List of length 1 and cutoff of 7\nExpected output for original List after call to lst_filter_leq:\n"); LIST *leq_small = lst_filter_leq(small, 5); printf("[ 7 ]\n"); printf("Your output of original List:\n"); lst_print(small); printf("Expected output for new list of leq elements:\n"); printf("[ ]\n"); printf("Your output of new list of leq elements:\n"); lst_print(leq_small); printf("\n\nTest 13C\nDescription: Testing lst_filter_leq with List of length 0 and cutoff of 5\nExpected output for original List after call to lst_filter_leq:\n"); LIST *leq_large = lst_filter_leq(large, 5); printf("[ 6 7 8 9 ]\n"); printf("Your output of original List:\n"); lst_print(large); printf("Expected output for new list of leq elements:\n"); printf("[ 0 1 2 3 4 5 ]\n"); printf("Your output of new list of leq elements:\n"); lst_print(leq_large); FREE(leq_empty); FREE(leq_small); FREE(leq_large); FREE(empty); FREE(small); FREE(large); }