diff --git a/Makefile b/Makefile index 10bd639..2880543 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ NAME = containers NAME_FT = containers_ft NAME_STL = containers_stl -CC = g++ +CC = clang++ EXT = cpp CFLAGS = -Wall -Wextra -Werror $(INCLUDES) @@ -61,14 +61,14 @@ D_SRCS = ./tests #SRCS = main42.cpp #SRCS = main_map_1.cpp #SRCS = main_map_2.cpp -SRCS = main_stack_1.cpp -#SRCS = \ -# main.cpp \ -# tests_definitions.cpp \ -# \ -# tests_vector.cpp \ -# tests_map.cpp \ -# tests_stack.cpp +#SRCS = main_stack_1.cpp +SRCS = \ + main.cpp \ + tests_definitions.cpp \ + \ + tests_vector.cpp \ + tests_map.cpp \ + tests_stack.cpp D_HEADERS = ./headers HEADERS = \ @@ -124,13 +124,13 @@ $(D_OBJS_STL)/%.o: %.$(EXT) | $(D_OBJS_STL) $(D_OBJS_FT) $(D_OBJS_STL): mkdir $@ -$(OBJS): $(F_INCLUDES) +$(OBJS_FT) $(OBJS_STL): $(F_INCLUDES) # https://stackoverflow.com/questions/19259108/makefile-same-rule-for-multiple-targets $(NAME_FT): $(OBJS_FT) $(NAME_STL): $(OBJS_STL) $(NAME_FT) $(NAME_STL): - @echo $(CYAN)"linkage (link objects.o)"$(RESET) + @echo $(PURPLE)"linkage (link objects.o)"$(RESET) @$(CC) $^ -o $@ $(LIBS) leaks: leaksft diff --git a/containers_ft b/containers_ft index 752db1a..07d34f3 100755 Binary files a/containers_ft and b/containers_ft differ diff --git a/containers_stl b/containers_stl new file mode 100755 index 0000000..88022da Binary files /dev/null and b/containers_stl differ diff --git a/headers/lexicographical_compare.hpp b/headers/lexicographical_compare.hpp index c1374c0..f42861f 100644 --- a/headers/lexicographical_compare.hpp +++ b/headers/lexicographical_compare.hpp @@ -6,34 +6,15 @@ namespace ft { template bool lexicographical_compare - ( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { + ( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 ) { - while (first1 != last1) - { - if (first2 == last2 || *first2 < *first1) - return false; - else if (*first1 < *first2) + for (; first1 != last1; first1++, first2++) { + if (*first1 < *first2) return true; - ++first1; - ++first2; - } - return (first2 != last2); -} - -template - bool lexicographical_compare - ( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp) { - - while (first1 != last1) - { - if (first2 == last2 || comp(*first2, *first1)) + if (*first2 < *first1) return false; - else if (comp(*first1, *first2)) - return true; - ++first1; - ++first2; } - return (first2 != last2); + return (first1 == last1 && first2 != last2); } } // namespace ft diff --git a/headers/stack.hpp b/headers/stack.hpp index 6ef6393..364f098 100644 --- a/headers/stack.hpp +++ b/headers/stack.hpp @@ -9,8 +9,8 @@ namespace ft { template < typename T, typename Container = ft::vector -> class stack -{ +> class stack { + public: typedef Container container_type; typedef typename Container::value_type value_type; @@ -22,7 +22,6 @@ public: ************/ // constructors ------------------------------ explicit stack(const container_type& cont = Container()) : c(cont) {} - explicit stack(stack const &other): c(other.c) {} /********************** @@ -55,7 +54,7 @@ public: friend bool operator>=(const stack& lhs, const stack& rhs); protected: - Container c; + container_type c; }; diff --git a/tests/includes/main.hpp b/tests/includes/main.hpp index abdfde7..6ea460f 100644 --- a/tests/includes/main.hpp +++ b/tests/includes/main.hpp @@ -73,6 +73,11 @@ void tests_map_relational_operators(); void tests_map_swap_non_member(); // stack void tests_stack_constructor(); +void tests_stack_empty(); +void tests_stack_size(); +void tests_stack_top(); +void tests_stack_push(); +void tests_stack_pop(); #endif diff --git a/tests/includes/tests_utils.hpp b/tests/includes/tests_utils.hpp index 7dc0d4c..b822a0a 100644 --- a/tests/includes/tests_utils.hpp +++ b/tests/includes/tests_utils.hpp @@ -41,6 +41,7 @@ // prototypes // ********************************************* +// abstract class test ----------------------- struct A_test { virtual ~A_test(){}; @@ -48,6 +49,7 @@ struct A_test std::string type; virtual void func() = 0; }; +// mystruct ---------------------------------- struct mystruct { public: mystruct(int data = 0); @@ -57,6 +59,7 @@ private: int * _val; }; std::ostream & operator<<(std::ostream & o, mystruct const * rhs); +// functions --------------------------------- void add_to_list(std::string title, std::string type, A_test* test); void delete_structs(); @@ -70,17 +73,19 @@ extern std::vector< mystruct* > mem_list; // adding each test to the list // *************************** #define TEST(f_name) TEST_V(f_name) + /* - add_to_list(#f_name, "char", new(s_ ## f_name ));\ - add_to_list(#f_name, "std::string", new(s_ ## f_name ));\ - add_to_list(#f_name, "mystruct*", new(s_ ## f_name ));\ */ + #define TEST_V(f_name) \ template struct s_ ## f_name : public A_test\ { void func(); };\ void f_name () {\ add_to_list("", "", NULL);\ add_to_list(#f_name, "int", new(s_ ## f_name ));\ + add_to_list(#f_name, "char", new(s_ ## f_name ));\ + add_to_list(#f_name, "std::string", new(s_ ## f_name ));\ + add_to_list(#f_name, "mystruct*", new(s_ ## f_name ));\ }\ template \ void s_ ## f_name ::func() @@ -93,16 +98,15 @@ extern std::vector< mystruct* > mem_list; add_to_list(#f_name, "char, int", new(s_ ## f_name ));\ add_to_list(#f_name, "char, char", new(s_ ## f_name ));\ add_to_list(#f_name, "char, std::string", new(s_ ## f_name ));\ - add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name ));\ add_to_list(#f_name, "int, int", new(s_ ## f_name ));\ add_to_list(#f_name, "int, char", new(s_ ## f_name ));\ add_to_list(#f_name, "int, std::string", new(s_ ## f_name ));\ - add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name ));\ }\ template \ void s_ ## f_name ::func() - /* + add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name ));\ + add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name ));\ */ // templates print diff --git a/tests/main.cpp b/tests/main.cpp index 5f27d3c..ac6c4c4 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -4,87 +4,68 @@ int main() { // VECTOR -// tests_vector_constructor(); -// tests_vector_operator_assignation(); -// tests_vector_begin(); -// tests_vector_end(); -// tests_vector_rbegin(); -// tests_vector_rend(); -// tests_vector_size(); -// tests_vector_max_size(); -// tests_vector_resize(); -// tests_vector_capacity(); -// tests_vector_empty(); -// tests_vector_reserve(); -// tests_vector_operator_access(); -// tests_vector_at(); -// tests_vector_front(); -// tests_vector_back(); -// tests_vector_assign(); -// tests_vector_push_back(); -// tests_vector_pop_back(); -// tests_vector_insert(); -// tests_vector_erase(); -// tests_vector_swap(); -// tests_vector_clear(); -// tests_vector_get_allocator(); -// tests_vector_relational_operators(); -// tests_vector_swap_non_member(); -// tests_vector_reverse_iterators(); + tests_vector_constructor(); + tests_vector_operator_assignation(); + tests_vector_begin(); + tests_vector_end(); + tests_vector_rbegin(); + tests_vector_rend(); + tests_vector_size(); + tests_vector_max_size(); + tests_vector_resize(); + tests_vector_capacity(); + tests_vector_empty(); + tests_vector_reserve(); + tests_vector_operator_access(); + tests_vector_at(); + tests_vector_front(); + tests_vector_back(); + tests_vector_assign(); + tests_vector_push_back(); + tests_vector_pop_back(); + tests_vector_insert(); + tests_vector_erase(); + tests_vector_swap(); + tests_vector_clear(); + tests_vector_get_allocator(); + tests_vector_swap_non_member(); + tests_vector_reverse_iterators(); + tests_vector_relational_operators(); // MAP -// tests_map_simple(); -// tests_map_constructor(); -// tests_map_operator_assignation(); -// tests_map_begin(); -// tests_map_end(); -// tests_map_rbegin(); -// tests_map_rend(); -// tests_map_empty(); -// tests_map_size(); -// tests_map_max_size(); -// tests_map_operator_access(); -// tests_map_insert(); -// tests_map_erase(); -// tests_map_swap(); -// tests_map_clear(); -// tests_map_key_comp(); -// tests_map_value_comp(); -// tests_map_find(); -// tests_map_count(); -// tests_map_lower_bound(); -// tests_map_upper_bound(); -// tests_map_equal_range(); -// tests_map_get_allocator(); -// tests_map_relational_operators(); -// tests_map_swap_non_member(); - + tests_map_simple(); + tests_map_constructor(); + tests_map_operator_assignation(); + tests_map_begin(); + tests_map_end(); + tests_map_rbegin(); + tests_map_rend(); + tests_map_empty(); + tests_map_size(); + tests_map_max_size(); + tests_map_operator_access(); + tests_map_insert(); + tests_map_erase(); + tests_map_swap(); + tests_map_clear(); + tests_map_key_comp(); + tests_map_value_comp(); + tests_map_find(); + tests_map_count(); + tests_map_lower_bound(); + tests_map_upper_bound(); + tests_map_equal_range(); + tests_map_get_allocator(); + tests_map_swap_non_member(); + tests_map_relational_operators(); // STACK tests_stack_constructor(); -// tests_stack_operator_assignation(); -// tests_stack_begin(); -// tests_stack_end(); -// tests_stack_rbegin(); -// tests_stack_rend(); -// tests_stack_size(); -// tests_stack_max_size(); -// tests_stack_resize(); -// tests_stack_capacity(); -// tests_stack_empty(); -// tests_stack_reserve(); -// tests_stack_operator_access(); -// tests_stack_at(); -// tests_stack_front(); -// tests_stack_back(); -// tests_stack_assign(); -// tests_stack_push_back(); -// tests_stack_pop_back(); -// tests_stack_insert(); -// tests_stack_erase(); -// tests_stack_swap(); -// tests_stack_clear(); -// tests_stack_get_allocator(); + tests_stack_empty(); + tests_stack_size(); + tests_stack_top(); + tests_stack_push(); + tests_stack_pop(); // execute tests and print them : int size = test_list.size(); diff --git a/tests/test.sh b/tests/test.sh index 219ffb9..e06248c 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -5,7 +5,8 @@ TEST_DIR=$(dirname $0) OUTPUT_STL="output_stl.log" OUTPUT_FT="output_ft.log" -make > /dev/null +#make -j > /dev/null +make -j echo -e "\nstl :" time ./containers_ft > tests/$OUTPUT_STL @@ -15,4 +16,4 @@ time ./containers_stl > tests/$OUTPUT_FT diff --context=0 --color=always tests/$OUTPUT_STL tests/$OUTPUT_FT -/bin/rm $TEST_DIR/$OUTPUT_STL $TEST_DIR/$OUTPUT_FT +#/bin/rm $TEST_DIR/$OUTPUT_STL $TEST_DIR/$OUTPUT_FT diff --git a/tests/tests_definitions.cpp b/tests/tests_definitions.cpp index ed3fbd4..8cd6937 100644 --- a/tests/tests_definitions.cpp +++ b/tests/tests_definitions.cpp @@ -36,13 +36,10 @@ void delete_structs() { // *********************************************** mystruct::mystruct(int data) {_val = new int[2]; _val[0] = data; _val[1] = data;} - mystruct::~mystruct() {delete[] _val;} - int * mystruct::get_data() const {return _val;} - std::ostream & operator<<(std::ostream & o, mystruct const * rhs) { if (rhs != NULL) o << (*rhs).get_data()[0] << "," << (*rhs).get_data()[1]; diff --git a/tests/tests_stack.cpp b/tests/tests_stack.cpp index ea3099d..4dbd706 100644 --- a/tests/tests_stack.cpp +++ b/tests/tests_stack.cpp @@ -1,29 +1,135 @@ #include "tests_utils.hpp" +#ifdef STL + #define DEQ_VEC deque +#else + #define DEQ_VEC vector +#endif + TEST(tests_stack_constructor) { // title TITLE(simple test) - std::deque mydeque (3,VAL(100)); // deque with 3 elements - std::vector myvector (2,VAL(200)); // vector with 2 elements + ft::DEQ_VEC mycont (2,VAL(200)); // ft::vector/stl::deque with 2 elements + ft::vector myvector (2,VAL(200)); // vector with 2 elements ft::stack first; // empty stack - ft::stack second (mydeque); // stack initialized to copy of deque + ft::stack second (mycont); // stack initialized to copy of vector -// ft::stack< T,std::vector > third; // empty stack using vector -// ft::stack< T,std::vector > fourth (myvector); -// -// std::cout << "size of first: " << first.size() << '\n'; -// std::cout << "size of second: " << second.size() << '\n'; -// std::cout << "size of third: " << third.size() << '\n'; -// std::cout << "size of fourth: " << fourth.size() << '\n'; -// -// PRINT(first) -// PRINT(second) -// PRINT(third) -// PRINT(fourth) -// -// DELETE + ft::stack< T,ft::vector > third; // empty stack using vector + ft::stack< T,ft::vector > fourth (myvector); + + std::cout << "size of first: " << first.size() << '\n'; + std::cout << "size of second: " << second.size() << '\n'; + std::cout << "size of third: " << third.size() << '\n'; + std::cout << "size of fourth: " << fourth.size() << '\n'; + + PRINT(first) + PRINT(second) + PRINT(third) + PRINT(fourth) + + DELETE +} + +TEST(tests_stack_empty) +{ + // title + TITLE(simple test) + + ft::stack mystack; + int sum (0); + + for (int i=1;i<=10;i++) mystack.push(VAL(i)); + + while (!mystack.empty()) + { + sum += TOI(mystack.top()); + mystack.pop(); + } + + std::cout << "total: " << sum << '\n'; + + PRINT(mystack) + + DELETE +} + +TEST(tests_stack_size) +{ + // title + TITLE(simple test) + + ft::stack myints; + std::cout << "0. size: " << myints.size() << '\n'; + + for (int i=0; i<5; i++) myints.push(VAL(i)); + std::cout << "1. size: " << myints.size() << '\n'; + + myints.pop(); + std::cout << "2. size: " << myints.size() << '\n'; + + PRINT(myints) + + DELETE +} + +TEST(tests_stack_top) +{ + // title + TITLE(simple test) + + ft::stack mystack; + + mystack.push(VAL(10)); + std::cout << "mystack.top() is now " << mystack.top() << '\n'; + mystack.push(VAL(20)); + std::cout << "mystack.top() is now " << mystack.top() << '\n'; + mystack.push(VAL(-12)); + std::cout << "mystack.top() is now " << mystack.top() << '\n'; + mystack.push(VAL(26)); + std::cout << "mystack.top() is now " << mystack.top() << '\n'; + +// mystack.top() -= VAL(5); + + + PRINT(mystack) + + DELETE +} + +TEST(tests_stack_push) +{ + // title + TITLE(simple test) + + ft::stack mystack; + + for (int i=0; i<5; ++i) mystack.push(VAL(i)); + + PRINT(mystack) + + DELETE +} + +TEST(tests_stack_pop) +{ + // title + TITLE(simple test) + + ft::stack mystack; + + for (int i=0; i<5; ++i) mystack.push(VAL(i)); + + std::cout << "Popping out elements..."; + while (!mystack.empty()) + { + std::cout << ' ' << mystack.top(); + mystack.pop(); + } + std::cout << '\n'; + + DELETE }