Update test_list.cpp
Tests are passing
@@ -24,6 +24,17 @@ bool iterate_int(void *data)
return true;
}
+bool iterate_string(void *data)
+{
+ char *string = *(char **)data;
+ printf("Found string value: %s\n", string);
+ return true;
+}
+
+void free_string(void *data)
+{
+ free(*(char **)data);
+}
BOOST_AUTO_TEST_SUITE(test_list)
@@ -37,38 +48,44 @@ BOOST_AUTO_TEST_CASE(test_create_delete) {
}
-struct Fixture{
- Fixture() {
- list = NULL;
+BOOST_AUTO_TEST_CASE(test_int_list){
+
+ int i, numbers = 10;
+ list_t *list = NULL;
list = create_list(sizeof(int), NULL);
+
+ for(i = 1; i <= numbers; i++) {
+ append_list(list, &i);
}
- ~Fixture() {
+ BOOST_CHECK(size_list(list) == 10);
+
+ for_each_list(list, iterate_int);
+
delete_list(list);
}
- list_t *list;
-};
-BOOST_FIXTURE_TEST_CASE(test_list_append, Fixture){
+BOOST_AUTO_TEST_CASE(test_string_list) {
+ int i, numNames = 5;
+ const char *names[] = { "David", "Kevin", "Michael", "Craig", "Jimi" };
-int i, numbers = 10;
+ list_t *list = NULL;
- for(i = 1; i <= numbers; i++) {
- append_list(list, &i);
- }
- BOOST_CHECK(size_list(list) == 10);
+ list = create_list(sizeof(char *), free_string);
+
+ char *name;
+ for (i = 0; i < numNames; i++) {
+ name = _strdup(names[i]);
+ append_list(list, &name);
}
-BOOST_FIXTURE_TEST_CASE(test_list_foreach, Fixture) {
+ BOOST_CHECK(size_list(list) == 5);
- int i, numbers = 10;
+ for_each_list(list, iterate_string);
- for (i = 1; i <= numbers; i++) {
- append_list(list, &i);
+ delete_list(list);
}
- for_each_list(list, iterate_int);
-}
BOOST_AUTO_TEST_SUITE_END()
