Skip to content

Commit

Permalink
rewritten tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kay Freyer committed Nov 12, 2024
1 parent e731f0e commit 4d73f58
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 195 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env zsh

GTEST_COLOR=1
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ bear: $(OBJ_FILES) $(TEST_OBJ_FILES)

test-fsanitize: $(TEST_TARGET)-FSANITIZE
- $(TEST_TARGET)-FSANITIZE
test:
- cmake -S . -B build
- cmake --build build
- cd build && ctest -V

norminette:
norminette -R CheckForbiddenSourceHeader -R CheckDefine src
Expand Down
1 change: 1 addition & 0 deletions includes/test_libft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ extern "C" {
#include <cstdio>
#include <string.h>
#include <bsd/string.h>
#include <vector>

#endif // TEST_LIBFT_H
3 changes: 3 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
// ::testing::GTEST_FLAG(filter) = "FtLstaddBackTests/FtLstaddBackTest*";
::testing::GTEST_FLAG(filter) = "FtLstaddFrontTest*";

return RUN_ALL_TESTS();
}
102 changes: 40 additions & 62 deletions tests/test_ft_lstadd_back.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
// #include "test_libft.hpp"

// t_list *create_list_2(int length) {
// if (length <= 0) {
// return NULL;
// }
// t_list *lst = new t_list{.content = (void*)1, .next = nullptr};
// t_list *head = lst;
// while (length > 1) {
// head->next = new t_list{.content = (void*)1, .next = nullptr};
// head = head->next;
// length--;
// }

// const char* last = "last";
// head->content = (void*)last;
// return lst;
// }

// void clear_list_2(t_list *lst) {
// while (lst) {
// t_list *tmp = lst;
// lst = lst->next;
// delete tmp;
// }
// }

// struct FtLstaddBackParams {
// t_list* lst;
// t_list* new_node;
// };


// class FtLstaddBackTest : public testing::TestWithParam<FtLstaddBackParams> {};


// TEST_P(FtLstaddBackTest, HandleVariousInputs) {
// FtLstaddBackParams params = GetParam();
// t_list *lst = params.lst;
// t_list *new_node = params.new_node;
// ft_lstadd_back(&lst, new_node);

// t_list *head = lst;
// while (head->next) {
// head = head->next;
// }
// EXPECT_STREQ((char*)(head->content), "new");
// clear_list_2(lst);
// }

// INSTANTIATE_TEST_SUITE_P(
// FtLstaddBackTests,
// FtLstaddBackTest,
// ::testing::Values(
// // FtLstlastParams{create_list_last_element_eq_length(0)},
// // FtLstlastParams{create_list_last_element_eq_length(1)},
// // FtLstlastParams{create_list_last_element_eq_length(5)},
// FtLstaddBackParams{create_list_2(0), new t_list{.content = (void *)"new", .next=nullptr}},
// FtLstaddBackParams{create_list_2(1), new t_list{.content = (void *)"new", .next=nullptr}},
// FtLstaddBackParams{create_list_2(3), new t_list{.content = (void *)"new", .next=nullptr}}
// )
// );
#include "test_libft.hpp"

struct FtLstaddBackParams {
std::vector<int> lst_entries;
};

class FtLstaddBackTest : public testing::TestWithParam<FtLstaddBackParams> {};

TEST_P(FtLstaddBackTest, HandleVariousInputs) {
FtLstaddBackParams params = GetParam();

std::vector<int> lst_entries = params.lst_entries;
t_list* lst = NULL;
for (int i = 0; i < lst_entries.size() ; i++ ) {
ft_lstadd_back(&lst, ft_lstnew(&lst_entries[i]));
}

t_list* head = lst;
int i = 0;
while (head) {
EXPECT_EQ(lst_entries[i++], *(int*)(head->content));
head = head->next;
}

head = lst;
while (head) {
lst = head;
head = head->next;
free(lst);
}
}

INSTANTIATE_TEST_SUITE_P(
FtLstaddBackTests,
FtLstaddBackTest,
::testing::Values(
FtLstaddBackParams{{1, 2, 3}},
FtLstaddBackParams{{}}
)
);
59 changes: 35 additions & 24 deletions tests/test_ft_lstaddfront.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
// #include "test_libft.hpp"
#include "test_libft.hpp"
#include <gtest/gtest.h>

// struct FtLstaddFrontParams {
// t_list **lst;
// t_list *new_node;
// };
struct FtLstaddFrontParams {
std::vector<int> lst_entries;
std::vector<int> new_entries;
};

// class FtLstaddFrontTest : public testing::TestWithParam<std::tuple<t_list*, t_list*>> {};
class FtLstaddFrontTest : public testing::TestWithParam<FtLstaddFrontParams> {};

// INSTANTIATE_TEST_SUITE_P(
// FtLstaddFrontTests, FtLstaddFrontTest,
// ::testing::Values(
// std::make_tuple(new t_list{.content = (void *)1, .next = nullptr},
// new t_list{.content = (void *)2, .next = nullptr}),
// std::make_tuple(new t_list{.content=(void*)3, .next=nullptr},
// new t_list{.content=(void*)4, .next=nullptr})
// )
// );
INSTANTIATE_TEST_SUITE_P(
FtLstaddFrontTests, FtLstaddFrontTest,
::testing::Values(
FtLstaddFrontParams{{1, 2, 3}, {4}},
FtLstaddFrontParams{{1, 2, 3}, {4, 5}},
FtLstaddFrontParams{{}, {4, 5}}
)
);

// TEST_P(FtLstaddFrontTest, FtTestLstaddFront) {
// t_list* lst = std::get<0>(GetParam());
// t_list* newElem = std::get<1>(GetParam());
// t_list* old_lst = lst;

// ft_lstadd_front(&lst, newElem);
TEST_P(FtLstaddFrontTest, FtTestLstaddFront) {
FtLstaddFrontParams params = GetParam();

// EXPECT_EQ(lst, newElem);
// EXPECT_EQ(newElem->next, old_lst);
std::vector<int> lst_entries = params.lst_entries;
t_list* lst = NULL;
for (int i = 0; i < lst_entries.size() ; i++ ) {
ft_lstadd_back(&lst, ft_lstnew(&lst_entries[i]));
}

// // ft_lstclear(&lst, free);
// }
std::vector<int> new_entries = params.new_entries;
for (int i = 0; i < new_entries.size() ; i++ ) {
ft_lstadd_front(&lst, ft_lstnew(&new_entries[i]));
EXPECT_EQ(new_entries[i], *(int*)lst->content);
}

t_list* head = lst;
while (head) {
lst = head;
head = head->next;
free(lst);
}
}
103 changes: 40 additions & 63 deletions tests/test_ft_lstlast.cpp
Original file line number Diff line number Diff line change
@@ -1,63 +1,40 @@
// #include "test_libft.hpp"


// t_list *create_list_last_element_eq_length(int length) {
// if (length <= 0) {
// return NULL;
// }
// t_list *lst = new t_list{.content = (void*)1, .next = nullptr};
// t_list *head = lst;
// while (length > 1) {
// head->next = new t_list{.content = (void*)1, .next = nullptr};
// head = head->next;
// length--;
// }

// const char* last = "last";
// head->content = (void*)last;
// return lst;
// }

// void clear_list_3(t_list *lst) {
// while (lst) {
// t_list *tmp = lst;
// lst = lst->next;
// delete tmp;
// }
// }

// struct FtLstlastParams {
// t_list* lst;
// };


// class FtLstlastTest : public testing::TestWithParam<FtLstlastParams> {
// public:
// ~FtLstlastTest() {
// FtLstlastParams params = GetParam();
// clear_list_3(params.lst);
// }
// };


// TEST_P(FtLstlastTest, HandleVariousInputs) {
// FtLstlastParams params = GetParam();
// t_list *lst = params.lst;
// t_list *got = ft_lstlast(lst);
// if (lst) {
// EXPECT_STREQ((char*)(got->content), "last");
// } else {
// EXPECT_EQ(got, nullptr);
// }
// }

// INSTANTIATE_TEST_SUITE_P(
// FtLstlastTests,
// FtLstlastTest,
// ::testing::Values(
// FtLstlastParams{create_list_last_element_eq_length(0)},
// FtLstlastParams{create_list_last_element_eq_length(1)},
// FtLstlastParams{create_list_last_element_eq_length(5)},
// FtLstlastParams{create_list_last_element_eq_length(3)}
// )
// );
#include "test_libft.hpp"

struct FtLstlastParams {
std::vector<int> lst_entries;
};


class FtLstlastTest : public testing::TestWithParam<FtLstlastParams> {};

TEST_P(FtLstlastTest, HandleVariousInputs) {
FtLstlastParams params = GetParam();

std::vector<int> lst_entries = params.lst_entries;
t_list* lst = NULL;
for (int i = 0; i < lst_entries.size() ; i++ ) {
ft_lstadd_back(&lst, ft_lstnew(&lst_entries[i]));
}

if (lst_entries.size()) {
EXPECT_EQ(lst_entries[lst_entries.size() - 1], *(int*)ft_lstlast(lst)->content);
} else {
EXPECT_EQ(nullptr, ft_lstlast(lst));
}

t_list* head = lst;
while (head) {
lst = head;
head = head->next;
free(lst);
}
}

INSTANTIATE_TEST_SUITE_P(
FtLstlastTests,
FtLstlastTest,
::testing::Values(
FtLstlastParams{{1, 2, 3}},
FtLstlastParams{{}}
)
);
36 changes: 18 additions & 18 deletions tests/test_ft_lstnew.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// #include "test_libft.hpp"
#include "test_libft.hpp"

// struct FtLstnewParams {
// void* content;
// };
struct FtLstnewParams {
void* content;
};

// class FtLstnewTest : public ::testing::TestWithParam<FtLstnewParams> {};
class FtLstnewTest : public ::testing::TestWithParam<FtLstnewParams> {};

// TEST_P(FtLstnewTest, HandlesVariousContents) {
// FtLstnewParams params = GetParam();
TEST_P(FtLstnewTest, HandlesVariousContents) {
FtLstnewParams params = GetParam();

// t_list *n = ft_lstnew(params.content);
// EXPECT_EQ(n->next, (void*)0);
// EXPECT_EQ(n->content, params.content);
// free(n);
// }
t_list *n = ft_lstnew(params.content);
EXPECT_EQ(n->next, (void*)0);
EXPECT_EQ(n->content, params.content);
free(n);
}

// INSTANTIATE_TEST_SUITE_P(FtLstnewTests, FtLstnewTest,
// ::testing::Values(
// FtLstnewParams{(void*)0},
// FtLstnewParams{(void*)"Hello"}
// )
// );
INSTANTIATE_TEST_SUITE_P(FtLstnewTests, FtLstnewTest,
::testing::Values(
FtLstnewParams{(void*)0},
FtLstnewParams{(void*)"Hello"}
)
);
Loading

0 comments on commit 4d73f58

Please sign in to comment.