From b33f1ef973ed2dc5ed3c6fe90b039284dbe1fc98 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 16 May 2017 02:58:16 +0200 Subject: [PATCH] Tests complets de utils --- fish_shell/CMakeLists.txt | 2 +- fish_shell/fish_core.c | 3 - fish_shell/fish_core.h | 8 +- fish_shell/fish_utils.c | 1 - fish_shell/fish_utils.h | 18 +-- fish_shell_tests/CMakeLists.txt | 2 +- fish_shell_tests/FishCoreTests.cpp | 17 +-- fish_shell_tests/FishCoreTests.h | 15 -- fish_shell_tests/FishUtilsTests.cpp | 210 ++++++++++++++++++++++++++++ fish_shell_tests/main.cpp | 4 + 10 files changed, 238 insertions(+), 42 deletions(-) delete mode 100644 fish_shell_tests/FishCoreTests.h create mode 100644 fish_shell_tests/FishUtilsTests.cpp diff --git a/fish_shell/CMakeLists.txt b/fish_shell/CMakeLists.txt index 272f384..df0e5e8 100644 --- a/fish_shell/CMakeLists.txt +++ b/fish_shell/CMakeLists.txt @@ -4,5 +4,5 @@ project(fish_shell) set(CMAKE_C_STANDARD 99) set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") -file(GLOB SOURCE_FILES *) +set(SOURCE_FILES main.c fish_types.h fish_core.h fish_core.c fish_commands.c fish_commands.h fish_globbing.c fish_globbing.h fish_utils.c fish_utils.h) add_executable(fish ${SOURCE_FILES}) \ No newline at end of file diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index 8eb5213..0e7f0bf 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -10,9 +10,6 @@ #include "fish_core.h" #include "fish_globbing.h" -#define FISH_BUFFER_SIZE 1024 -#define FISH_TOKENS " \t\r\n\a" - void fishLoop(Settings * settings){ char * line = NULL; WordList* splited = NULL; diff --git a/fish_shell/fish_core.h b/fish_shell/fish_core.h index 2b8cad1..27a51cf 100644 --- a/fish_shell/fish_core.h +++ b/fish_shell/fish_core.h @@ -5,6 +5,10 @@ #ifndef FISH_FISH_CORE_H #define FISH_FISH_CORE_H +#define FISH_BUFFER_SIZE 1024 +#define FISH_TOKENS " \t\r\n\a" + + #include "fish_types.h" #include "fish_commands.h" @@ -12,7 +16,7 @@ /* WordArray functions */ -WordList * split(char *string, char *separator); +WordList * split(char *string, char *separator); // Tested /* Settings functions */ @@ -26,7 +30,7 @@ void fishLoop(Settings * settings); char * fishReadLine(); -int countSeparators(char *string, char *separators); +int countSeparators(char *string, char *separators); // Tested int fishLoad(WordArray *array); diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index 7132ec2..50a5d4c 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -50,7 +50,6 @@ void freeWordArray(WordArray *array) { free(array->words); free(array); } - } diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index 70e3725..5aec4eb 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -7,22 +7,22 @@ #include "fish_types.h" -void crash(); +void crash(); // Tested -char *getInsult(); +char *getInsult(); // Tested -void freeWordArray(WordArray *array); +void freeWordArray(WordArray *array); // Tested -WordList * createWordList(); +WordList * createWordList(); // Tested -void addWordList(WordList *list, char *word); +void addWordList(WordList *list, char *word); // Tested -void removeWordList(WordList *list); +void removeWordList(WordList *list); // Tested -void freeWordList(WordList *list); +void freeWordList(WordList *list); // Tested -WordArray * wordListToWordArray(WordList *list); +WordArray * wordListToWordArray(WordList *list); // Tested -WordList * wordArrayToWordList(WordArray * array); +WordList * wordArrayToWordList(WordArray * array); // Tested #endif //FISH_FISH_UTILS_H diff --git a/fish_shell_tests/CMakeLists.txt b/fish_shell_tests/CMakeLists.txt index 72e4047..a634843 100644 --- a/fish_shell_tests/CMakeLists.txt +++ b/fish_shell_tests/CMakeLists.txt @@ -11,7 +11,7 @@ include_directories( "${source_dir}/googlemock/include" ) -file(GLOB SOURCE_FILES_TESTS *) +set(SOURCE_FILES_TESTS main.cpp FishCoreTests.cpp FishUtilsTests.cpp) add_executable(fish_tests ${SOURCE_FILES_TESTS}) diff --git a/fish_shell_tests/FishCoreTests.cpp b/fish_shell_tests/FishCoreTests.cpp index 4ef96b8..b90efc3 100644 --- a/fish_shell_tests/FishCoreTests.cpp +++ b/fish_shell_tests/FishCoreTests.cpp @@ -2,16 +2,9 @@ // Created by Antoine Bartuccio on 15/05/2017. // -#include "FishCoreTests.h" -#include "../fish_shell/fish_core.c" -#include "../fish_shell/fish_utils.c" -#include "../fish_shell/fish_commands.c" -#include "../fish_shell/fish_globbing.c" - - -//TEST_F(FishCoreTests, split){ -// ASSERT_TRUE(true); -//} +#include "gtest/gtest.h" +#include "../fish_shell/fish_types.h" +#include "../fish_shell/fish_core.h" TEST(command_split, split){ char input[] = "git push --force"; @@ -34,3 +27,7 @@ TEST(command_split, split){ freeWordList(list); } + +TEST(count_tokens, countSeparators){ + ASSERT_TRUE(countSeparators((char*) "Ceci est un super \n test", (char*) FISH_TOKENS) == 6); +} diff --git a/fish_shell_tests/FishCoreTests.h b/fish_shell_tests/FishCoreTests.h deleted file mode 100644 index 45ce012..0000000 --- a/fish_shell_tests/FishCoreTests.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by Antoine Bartuccio on 15/05/2017. -// - -#ifndef FISH_FISH_CORE_TESTS_H -#define FISH_FISH_CORE_TESTS_H - -#include - -class FishCoreTests : public ::testing::Test { - void SetUp() {} - void TearDown(){} -}; - -#endif //FISH_FISH_CORE_TESTS_H diff --git a/fish_shell_tests/FishUtilsTests.cpp b/fish_shell_tests/FishUtilsTests.cpp new file mode 100644 index 0000000..5232497 --- /dev/null +++ b/fish_shell_tests/FishUtilsTests.cpp @@ -0,0 +1,210 @@ +// +// Created by Antoine Bartuccio on 16/05/2017. +// + +#include "gtest/gtest.h" +#include "../fish_shell/fish_utils.h" +#include "../fish_shell/fish_types.h" + +TEST(crash_test, crash){ + ASSERT_DEATH(crash(), "[A-z]+"); +} + +TEST(get_an_insult, getInsult){ + ASSERT_EQ(typeid(char*), typeid(getInsult())); +} + +TEST(word_list_init, createWordList){ + WordList *list = createWordList(); + ASSERT_FALSE(list == NULL); + ASSERT_TRUE(list->first == NULL); + ASSERT_TRUE(list->last == NULL); + ASSERT_TRUE(list->size == 0); +} + +TEST(word_list_add, addWordList){ + WordList *list = createWordList(); + char test1[] = "Test1"; + char test2[] = "Test2"; + char test3[] = "Test3"; + + addWordList(list, test1); + ASSERT_TRUE(list->size == 1); + ASSERT_TRUE(list->first != NULL); + ASSERT_TRUE(list->last != NULL); + ASSERT_TRUE(list->first == list->last); + ASSERT_TRUE(list->first->word != NULL); + ASSERT_FALSE(strcmp(list->first->word, test1)); + ASSERT_FALSE(list->first->word == test1); + + addWordList(list, test2); + ASSERT_TRUE(list->size == 2); + ASSERT_TRUE(list->first != list->last); + ASSERT_FALSE(strcmp(list->last->word, test2)); + ASSERT_FALSE(strcmp(list->first->word, test1)); + ASSERT_TRUE(strcmp(list->first->word, test2)); + + addWordList(list, test3); + ASSERT_TRUE(list->size == 3); + ASSERT_TRUE(list->first != list->last); + ASSERT_FALSE(strcmp(list->first->word, test1)); + ASSERT_FALSE(strcmp(list->last->word, test3)); + ASSERT_TRUE(strcmp(list->last->word, test2)); + + freeWordList(list); +} + +TEST(word_list_remove, removeWordList){ + WordList *list = createWordList(); + char word[] = "lel"; + + addWordList(list, word); + addWordList(list, word); + addWordList(list, word); + + ASSERT_TRUE(list->size == 3); + + removeWordList(list); + + ASSERT_TRUE(list->size == 2); + + removeWordList(list); + + ASSERT_TRUE(list->size == 1); + + freeWordList(list); + +} + +TEST(word_list_free, freeWordList){ + WordList *list = createWordList(); + char word[] = "lel"; + addWordList(list, word); + addWordList(list, word); + addWordList(list, word); + + freeWordList(list); + + list = createWordList(); + + freeWordList(list); +} + +TEST(word_list_to_word_array, wordListToWordArray){ + WordList *list = createWordList(); + WordListElement *elem = NULL; + WordArray *array = NULL; + int i = 0; + + char test1[] = "Test1"; + char test2[] = "Test2"; + char test3[] = "Test3"; + + addWordList(list, test1); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test2); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test1); + addWordList(list, test1); + + array = wordListToWordArray(list); + ASSERT_FALSE(array == NULL); + ASSERT_FALSE(array->words == NULL); + + list = createWordList(); + addWordList(list, test1); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test2); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test1); + addWordList(list, test1); + + ASSERT_EQ(list->size, array->size); + + elem = list->first; + + for (i=0; isize; i++){ + ASSERT_FALSE(strcmp(array->words[i], elem->word)); + elem = elem->next; + } + + ASSERT_TRUE(array->words[array->size] == NULL); + + freeWordList(list); + freeWordArray(array); +} + +TEST(word_array_to_word_list, wordArrayToWordList){ + WordList *list = createWordList(); + WordList *list2 = NULL; + WordListElement *elem = NULL; + WordArray *array = NULL; + int i = 0; + + char test1[] = "Test1"; + char test2[] = "Test2"; + char test3[] = "Test3"; + + addWordList(list, test1); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test2); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test1); + addWordList(list, test1); + + array = wordListToWordArray(list); + list2 = wordArrayToWordList(array); + + list = createWordList(); + addWordList(list, test1); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test2); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test1); + addWordList(list, test1); + array = wordListToWordArray(list); + + ASSERT_TRUE(list2 != NULL); + ASSERT_EQ(array->size, list2->size); + + elem = list2->first; + + for (i=0;isize;i++){ + ASSERT_FALSE(strcmp(array->words[i], elem->word)); + elem = elem->next; + } + + freeWordList(list2); + freeWordArray(array); + +} + +TEST(word_array_free, freeWordArray){ + WordList *list = createWordList(); + WordArray *array = NULL; + + char test1[] = "Test1"; + char test2[] = "Test2"; + char test3[] = "Test3"; + + addWordList(list, test1); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test2); + addWordList(list, test2); + addWordList(list, test3); + addWordList(list, test1); + addWordList(list, test1); + + array = wordListToWordArray(list); + + freeWordArray(array); +} \ No newline at end of file diff --git a/fish_shell_tests/main.cpp b/fish_shell_tests/main.cpp index c2657e9..8260e08 100644 --- a/fish_shell_tests/main.cpp +++ b/fish_shell_tests/main.cpp @@ -3,6 +3,10 @@ // #include "gtest/gtest.h" +#include "../fish_shell/fish_core.c" +#include "../fish_shell/fish_utils.c" +#include "../fish_shell/fish_commands.c" +#include "../fish_shell/fish_globbing.c" int main(int argc, char **argv){ ::testing::InitGoogleTest(&argc, argv);