From 609a08480df3ce1b4737fcf8ce55be7a8f3ada56 Mon Sep 17 00:00:00 2001 From: Niklas Kaaf Date: Sat, 16 Dec 2023 11:34:36 +0100 Subject: [PATCH] Version 3.0.0 (#42) --- .clang-format | 66 ++++ .gitignore | 3 +- Doxyfile | 2 +- README.md | 20 +- .../DifferenceMutableAndImmutable.ino | 10 +- .../List/ManageElements/ManageElements.ino | 17 +- .../List/UtilsAndHelper/UtilsAndHelper.ino | 34 +- keywords.txt | 7 +- library.json | 4 +- library.properties | 2 +- src/AbstractList.hpp | 303 +++++++++--------- src/DoubleLinkedList.hpp | 86 ++--- src/List.hpp | 11 +- src/SingleLinkedList.hpp | 86 ++--- .../test_immutable_doublelinkedlist_+.cpp | 246 ++++++++++++++ .../test_immutable_doublelinkedlist_add.cpp | 184 +++++++++++ ...test_immutable_doublelinkedlist_addAll.cpp | 262 +++++++++++++++ ..._immutable_doublelinkedlist_addAtIndex.cpp | 252 +++++++++++++++ ...st_immutable_doublelinkedlist_addFirst.cpp | 182 +++++++++++ ...est:immutable_doublelinkedlist_addLast.cpp | 182 +++++++++++ ...est_immutable_doublelinkedlist_diverse.cpp | 112 +++++++ ...st_immutable_doublelinkedlist_equality.cpp | 216 +++++++++++++ .../test_immutable_doublelinkedlist_get.cpp | 122 +++++++ ...test_immutable_doublelinkedlist_remove.cpp | 244 ++++++++++++++ .../test_immutable_list_+.cpp | 246 ++++++++++++++ .../test_immutable_list_add.cpp | 184 +++++++++++ .../test_immutable_list_addAll.cpp | 262 +++++++++++++++ .../test_immutable_list_addAtIndex.cpp | 252 +++++++++++++++ .../test_immutable_list_addFirst.cpp | 182 +++++++++++ .../test:immutable_list_addLast.cpp | 182 +++++++++++ .../test_immutable_list_diverse.cpp | 112 +++++++ .../test_immutable_list_equality.cpp | 216 +++++++++++++ .../test_immutable_list_get.cpp | 122 +++++++ .../test_immutable_list_remove.cpp | 244 ++++++++++++++ .../test_immutable_singlelinkedlist_+.cpp | 246 ++++++++++++++ .../test_immutable_singlelinkedlist_add.cpp | 184 +++++++++++ ...test_immutable_singlelinkedlist_addAll.cpp | 262 +++++++++++++++ ..._immutable_singlelinkedlist_addAtIndex.cpp | 252 +++++++++++++++ ...st_immutable_singlelinkedlist_addFirst.cpp | 182 +++++++++++ ...est:immutable_singlelinkedlist_addLast.cpp | 182 +++++++++++ ...est_immutable_singlelinkedlist_diverse.cpp | 112 +++++++ ...st_immutable_singlelinkedlist_equality.cpp | 216 +++++++++++++ .../test_immutable_singlelinkedlist_get.cpp | 122 +++++++ ...test_immutable_singlelinkedlist_remove.cpp | 244 ++++++++++++++ 44 files changed, 6344 insertions(+), 313 deletions(-) create mode 100644 .clang-format create mode 100644 test/test_immutable_doublelinkedlist_+/test_immutable_doublelinkedlist_+.cpp create mode 100644 test/test_immutable_doublelinkedlist_add/test_immutable_doublelinkedlist_add.cpp create mode 100644 test/test_immutable_doublelinkedlist_addAll/test_immutable_doublelinkedlist_addAll.cpp create mode 100644 test/test_immutable_doublelinkedlist_addAtIndex/test_immutable_doublelinkedlist_addAtIndex.cpp create mode 100644 test/test_immutable_doublelinkedlist_addFirst/test_immutable_doublelinkedlist_addFirst.cpp create mode 100644 test/test_immutable_doublelinkedlist_addLast/test:immutable_doublelinkedlist_addLast.cpp create mode 100644 test/test_immutable_doublelinkedlist_diverse/test_immutable_doublelinkedlist_diverse.cpp create mode 100644 test/test_immutable_doublelinkedlist_equality/test_immutable_doublelinkedlist_equality.cpp create mode 100644 test/test_immutable_doublelinkedlist_get/test_immutable_doublelinkedlist_get.cpp create mode 100644 test/test_immutable_doublelinkedlist_remove/test_immutable_doublelinkedlist_remove.cpp create mode 100644 test/test_immutable_list_+/test_immutable_list_+.cpp create mode 100644 test/test_immutable_list_add/test_immutable_list_add.cpp create mode 100644 test/test_immutable_list_addAll/test_immutable_list_addAll.cpp create mode 100644 test/test_immutable_list_addAtIndex/test_immutable_list_addAtIndex.cpp create mode 100644 test/test_immutable_list_addFirst/test_immutable_list_addFirst.cpp create mode 100644 test/test_immutable_list_addLast/test:immutable_list_addLast.cpp create mode 100644 test/test_immutable_list_diverse/test_immutable_list_diverse.cpp create mode 100644 test/test_immutable_list_equality/test_immutable_list_equality.cpp create mode 100644 test/test_immutable_list_get/test_immutable_list_get.cpp create mode 100644 test/test_immutable_list_remove/test_immutable_list_remove.cpp create mode 100644 test/test_immutable_singlelinkedlist_+/test_immutable_singlelinkedlist_+.cpp create mode 100644 test/test_immutable_singlelinkedlist_add/test_immutable_singlelinkedlist_add.cpp create mode 100644 test/test_immutable_singlelinkedlist_addAll/test_immutable_singlelinkedlist_addAll.cpp create mode 100644 test/test_immutable_singlelinkedlist_addAtIndex/test_immutable_singlelinkedlist_addAtIndex.cpp create mode 100644 test/test_immutable_singlelinkedlist_addFirst/test_immutable_singlelinkedlist_addFirst.cpp create mode 100644 test/test_immutable_singlelinkedlist_addLast/test:immutable_singlelinkedlist_addLast.cpp create mode 100644 test/test_immutable_singlelinkedlist_diverse/test_immutable_singlelinkedlist_diverse.cpp create mode 100644 test/test_immutable_singlelinkedlist_equality/test_immutable_singlelinkedlist_equality.cpp create mode 100644 test/test_immutable_singlelinkedlist_get/test_immutable_singlelinkedlist_get.cpp create mode 100644 test/test_immutable_singlelinkedlist_remove/test_immutable_singlelinkedlist_remove.cpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..30dc265 --- /dev/null +++ b/.clang-format @@ -0,0 +1,66 @@ +# Generated from CLion C/C++ Code Style settings +BasedOnStyle: LLVM +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: None +AlignOperands: DontAlign +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Always +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 0 +CompactNamespaces: false +ContinuationIndentWidth: 4 +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 2 +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: false +PointerAlignment: Right +ReflowComments: true +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 0 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never diff --git a/.gitignore b/.gitignore index d9cac3d..1bc47d9 100644 --- a/.gitignore +++ b/.gitignore @@ -12,9 +12,8 @@ html # VSCode .vscode -# PlatformIO for testing +# PlatformIO .pio -test lib src/main.cpp include diff --git a/Doxyfile b/Doxyfile index e4bcd3f..7d2a63a 100644 --- a/Doxyfile +++ b/Doxyfile @@ -38,7 +38,7 @@ PROJECT_NAME = "Arduino List Library" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 2.1.4 +PROJECT_NUMBER = 3.0.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/README.md b/README.md index cd3b262..c64e837 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,13 @@ # List -![Build Status](https://github.com/nkaaf/Arduino-List/workflows/Arduino%20Library%20CI/badge.svg) -[![PlatformIO Registry](https://badges.registry.platformio.org/packages/nkaaf/library/List.svg)](https://registry.platformio.org/libraries/nkaaf/List) -[![arduino-library-badge](https://www.ardu-badge.com/badge/List.svg)](https://www.ardu-badge.com/List) +![Image indicating the latest Build Status](https://github.com/nkaaf/Arduino-List/workflows/Arduino%20Library%20CI/badge.svg "Build Status") +[![Image indicating the latest version in the PlatformIO Registry](https://badges.registry.platformio.org/packages/nkaaf/library/List.svg "PlatformIO Registry")](https://registry.platformio.org/libraries/nkaaf/List) +[![Image indicating the latest version in the Arduino Library Registry](https://www.ardu-badge.com/badge/List.svg "Arduino Library Registry")](https://www.ardu-badge.com/List) +## Intention Library to extend the Arduino Ecosystem with different List Implementations. -Documentation: [https://nkaaf.github.io/Arduino-List/](https://nkaaf.github.io/Arduino-List/) +## Documentation +The documentation can be found [here](https://nkaaf.github.io/Arduino-List/). -https://semver.org/ compliant - -## Arduino Library References - -* https://docs.arduino.cc/learn/contributions/arduino-writing-style-guide -* https://docs.arduino.cc/learn/contributions/arduino-library-style-guide -* https://arduino.github.io/arduino-cli/0.33/library-specification/ -* https://arduino.github.io/arduino-cli/0.33/sketch-specification/ +## Trivia +This library is [Semantic Versioning 2.0.0](https://semver.org/) compliant. diff --git a/examples/List/DifferenceMutableAndImmutable/DifferenceMutableAndImmutable.ino b/examples/List/DifferenceMutableAndImmutable/DifferenceMutableAndImmutable.ino index bbee11e..8063948 100644 --- a/examples/List/DifferenceMutableAndImmutable/DifferenceMutableAndImmutable.ino +++ b/examples/List/DifferenceMutableAndImmutable/DifferenceMutableAndImmutable.ino @@ -20,9 +20,9 @@ void setup() { // Get the first element of both lists Serial.print("The first element of the first list is: "); - Serial.println(mutableList.getValue(0)); + Serial.println(mutableList.get(0)); Serial.print("The first element of the second list is: "); - Serial.println(immutableList.getValue(0)); + Serial.println(immutableList.get(0)); Serial.println("All three values are logically the same!"); Serial.println(); @@ -35,12 +35,12 @@ void setup() { // Get the first element of both lists Serial.print("The first element of the first list is: "); - Serial.println(mutableList.getValue(0)); + Serial.println(mutableList.get(0)); Serial.print("The first element of the second list is: "); - Serial.println(immutableList.getValue(0)); + Serial.println(immutableList.get(0)); Serial.println("You see, that the first element of the first list has the the new value and the first element of the second list has not changed."); Serial.println("Elements of mutable lists can be manipulate via the same variable; immutable lists cannot!"); } -void loop() {} \ No newline at end of file +void loop() {} diff --git a/examples/List/ManageElements/ManageElements.ino b/examples/List/ManageElements/ManageElements.ino index 59ab4e3..faba64c 100644 --- a/examples/List/ManageElements/ManageElements.ino +++ b/examples/List/ManageElements/ManageElements.ino @@ -3,30 +3,27 @@ // Create an immutable list List list; +// TODO: rvalue void setup() { Serial.begin(9600); // Add the first element to the list int setup1 = 25; - list.add(setup1); // add() will always place the element at the end of the list. Its synonym is addLast(). addFirst() will place the element directly at the front of the list. + list.add(setup1);// add() will always place the element at the end of the list. Its synonym is addLast(). addFirst() will place the element directly at the front of the list. Serial.println("Inserted the first element"); Serial.println(); // Get the first element Serial.print("The value of the first element with the getValue() method is: "); - Serial.println(list.getValue(0)); // The most comfortable way to get the first elements value is to call the getValue() method. You cannot get the address of the element with the getValue() method! + Serial.println(list.get(0));// The most comfortable way to get the first elements value is to call the getValue() method. You cannot get the address of the element with the getValue() method! // Get the first element (alternative) Serial.print("The value of the first element with the [] operator is: "); - int firstElement = list[0]; // The '[]' Operator is a synonym for the getValue() method. + int firstElement = list[0];// The '[]' Operator is a synonym for the getValue() method. Serial.println(firstElement); - // Get the first element (alternative) - Serial.print("The value of the first element with the getPointer() method and '*' is: "); - int *firstElementPtr = list.getPointer(0); // Here, have to be the '*' to get the int Value, because otherwise a pointer (memory address) will be returned. - Serial.println(*firstElementPtr); - free(firstElementPtr); // free the pointer because it is an immutable list + // TODO: add getMutableValue for mutable lists Serial.println("As you can see, there are three possible ways to get the value. The last way is not for beginners because you have to handle pointers."); Serial.println(); @@ -45,7 +42,7 @@ void setup() { } // Remove element from list - list.remove(3); // With this, you will remove the third element of the list. + list.remove(3);// With this, you will remove the third element of the list. Serial.print("After the deletion of the third element, the list has: "); Serial.print(list.getSize()); Serial.println(" element(s)"); @@ -64,4 +61,4 @@ void setup() { Serial.println(" element(s)"); } -void loop() {} \ No newline at end of file +void loop() {} diff --git a/examples/List/UtilsAndHelper/UtilsAndHelper.ino b/examples/List/UtilsAndHelper/UtilsAndHelper.ino index 4fbd379..97946dd 100644 --- a/examples/List/UtilsAndHelper/UtilsAndHelper.ino +++ b/examples/List/UtilsAndHelper/UtilsAndHelper.ino @@ -35,23 +35,23 @@ void setup() { Serial.println("Inserted another element"); // Convert list to array - int *array = list.toArray(); - // Print every element from the new array - Serial.print("The list contains the following values which are get by converting the list to an array: ["); - for (int i = 0; i < list.getSize(); ++i) { - Serial.print(array[i]); - if (i != list.getSize() - 1) { // only add the ',' if the element is not the last; otherwise the output would be: [2,2,]; but now it is [2,2] - Serial.print(","); - } - } - Serial.println("]"); - // free memory space generated by toArray(), because it is not used anymore - free(array); + // int *array = list.toArray(); + //// Print every element from the new array + // Serial.print("The list contains the following values which are get by converting the list to an array: ["); + // for (int i = 0; i < list.getSize(); ++i) { + // Serial.print(array[i]); + // if (i != list.getSize() - 1) { // only add the ',' if the element is not the last; otherwise the output would be: [2,2,]; but now it is [2,2] + // Serial.print(","); + // } + // } + // Serial.println("]"); + //// free memory space generated by toArray(), because it is not used anymore + // free(array); Serial.println(); // Compare two lists - List secondList; // Create second list + List secondList;// Create second list if (list.equals(secondList)) { Serial.println("The two lists are identical."); } else { @@ -62,7 +62,7 @@ void setup() { secondList.add(setup2); // Check another time - if (list == secondList) { // The '==' operator is a synonym for the equals() method. + if (list == secondList) {// The '==' operator is a synonym for the equals() method. Serial.println("The two lists are identical."); } else { Serial.println("The two lists aren't identical."); @@ -73,7 +73,7 @@ void setup() { Serial.println("Add two more elements to the second list."); int setup3 = 12; int setup4 = 41; - secondList.add(setup3); // add two more values to the second List + secondList.add(setup3);// add two more values to the second List secondList.add(setup4); // Add one list to another @@ -84,11 +84,11 @@ void setup() { Serial.print("The list contains the following values: ["); for (int i = 0; i < list.getSize(); ++i) { Serial.print(list[i]); - if (i != list.getSize() - 1) { // only add the ',' if the element is not the last; otherwise the output would be: [2,2,]; but now it is [2,2] + if (i != list.getSize() - 1) {// only add the ',' if the element is not the last; otherwise the output would be: [2,2,]; but now it is [2,2] Serial.print(","); } } Serial.println("]"); } -void loop() {} \ No newline at end of file +void loop() {} diff --git a/keywords.txt b/keywords.txt index 25515d3..236c902 100644 --- a/keywords.txt +++ b/keywords.txt @@ -21,8 +21,8 @@ addAll KEYWORD2 addFirst KEYWORD2 addLast KEYWORD2 clear KEYWORD2 -getValue KEYWORD2 -getPointer KEYWORD2 +get KEYWORD2 +getMutableValue KEYWORD2 remove KEYWORD2 removeFirst KEYWORD2 removeLast KEYWORD2 @@ -30,7 +30,4 @@ removeAll KEYWORD2 getSize KEYWORD2 isMutable KEYWORD2 isEmpty KEYWORD2 -toArray KEYWORD2 -fromArray KEYWORD2 -sort KEYWORD2 equals KEYWORD2 diff --git a/library.json b/library.json index 93374f8..f8d4896 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "List", - "version": "2.1.4", + "version": "3.0.0", "description": "The Ultimate Collection of Lists. This library extends the Arduino ecosystem with the functionality of several easy-to-use lists for numerous purposes.", "keywords": [ "arduino", @@ -54,4 +54,4 @@ "LICENSE" ] } -} \ No newline at end of file +} diff --git a/library.properties b/library.properties index 2fc3bf7..4dcf179 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=List -version=2.1.4 +version=3.0.0 author=Niklas Kaaf maintainer=Niklas Kaaf sentence=The Ultimate Collection of Lists diff --git a/src/AbstractList.hpp b/src/AbstractList.hpp index 7e88e8e..afa0617 100644 --- a/src/AbstractList.hpp +++ b/src/AbstractList.hpp @@ -26,61 +26,57 @@ #ifndef LIST_ABSTRACT_LIST_HPP #define LIST_ABSTRACT_LIST_HPP -#include -#include - /*! * @brief Abstract class from which all lists can be derived. * * @tparam T Data Type of entries, that should be stored in the list. */ -template class AbstractList { -private: - int size = 0; /// Size of the list. - bool mutableList = false; /// Is the list mutable or immutable. +template +class AbstractList { + size_t size = 0; /// Size of the list. + bool mutableList = false;/// Is the list mutable or immutable. -protected: + protected: /// Sometimes it is allowed, that index == this->getSize() to insert it behind /// the last entry -#define extendedIsIndexOutOfBounds(index) \ +#define extendedIsIndexOutOfBounds(index) \ ((index) != this->getSize() && this->isIndexOutOfBounds(index)) - /// Create a final Value from the given Value -#define createFinalValue(value, finalValue, T) \ - finalValue = (T *)malloc(sizeof(T)); \ - memcpy(finalValue, &value, sizeof(T)); - /** * Class representing an abstract entry in the list. */ class AbstractEntry { - private: - T *value = nullptr; /// Pointer to the value. + T immutableValue; /// The raw value, assigned for immutable lists. + T *mutableValue = nullptr;/// A pointer to the raw value, assigned for mutable lists. - public: + public: /*! - * @brief Constructor of an AbstractEntry Object. + * @brief Get a pointer to the value (mutable or immutable). * - * @param value Value of the entry. - */ - explicit AbstractEntry(T *value) : value(value) {} - - /*! - * @brief Destructor of an AbstractEntry Object. - */ - ~AbstractEntry() { value = nullptr; } - - /*! - * @brief Free the memory of the value to prevent memory leaks. + * @param m Indicates, if the list is mutable or immutable. + * + * @return Pointer to the value of the entry. */ - void freeValue() { free(value); } + T *getValue(const bool m) { + if (m) { + return mutableValue; + } + return &immutableValue; + } /*! - * @brief Get the value of the entry. + * @brief Set the value. * - * @return Pointer to the value of the entry. + * @param val Reference to the value. + * @param m Indicates, if the list is mutable or immutable. */ - T *getValue() { return value; }; + void setValue(T &val, const bool m) { + if (m) { + mutableValue = &val; + } else { + immutableValue = val; + } + } }; /*! @@ -88,7 +84,21 @@ template class AbstractList { * * @param mutableList true if the list should be mutable; false otherwise. */ - explicit AbstractList(bool mutableList) : mutableList(mutableList) {} + explicit AbstractList(const bool mutableList) : mutableList(mutableList) {} + + /*! + * @brief Get a pointer to the element, stored at specific index. + * + * @param index The index of the element to retrieve. + * @return The nullptr, if the index is out of bounds, otherwise the address + * of the element. + * + * @note This is independent from the mutability of the list. It will always return the + * correct address (pointer) to the element. + * @note Allowed indices are 0 to getSize() -1. If the index is out of bounds, + * a nullptr will be returned. + */ + virtual T *getPointer(int index) = 0; /*! * @brief Increase the size of the list by one. Should only be called after an @@ -115,137 +125,171 @@ template class AbstractList { * @return true if the given index is in the range of the list; false * otherwise */ - bool isIndexOutOfBounds(int index) { return index < 0 || index >= getSize(); } + bool isIndexOutOfBounds(const int index) const { return index < 0 || index >= getSize(); } + public: /*! - * @brief Get a pointer to the entry at the given index. If the given index - * does not exists, null will be returned. - * @note If the list is immutable, the returned pointer has to be free'd with - * free() in order to prevent memory leaks. - * - * @param index Index of the element to get. - * @return Pointer to the element. - */ - virtual T *get(int index) = 0; - -public: - /*! - * @copybrief AbstractList::addLast() + * @copydoc AbstractList::addLast() * @note Alias of addLast(). + * @note If this list is mutable, ensure, that all variables added to the lists do not go out-of-scope during all operations of the list. * @see addLast() - * - * @param value Value to add. */ void add(T &value) { addLast(value); } +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) /*! * @copydoc AbstractList::add() + * @note If the list is mutable and the values saved in the list are not primitives, nothing happen. */ void add(T &&value) { addLast(value); } +#endif /*! * @brief Add the value to the list at the given index. The original entry at * this index, and followings, will be placed directly after the new * entry. - * @note Use this only if you know what you are doing. Otherwise use add(), - * addFirst() or addLast(). + * @note Allowed indices are 0 to getSize(). If the index is out of bounds, + * nothing will happen. + * @note If this list is mutable, ensure, that all variables added to the lists do not go out-of-scope during all operations of the list. * * @param index Index of the entry, where the value should be added. * @param value Value of the new entry. */ virtual void addAtIndex(int index, T &value) = 0; +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) /*! * @copydoc AbstractList::addAtIndex() + * @note If the list is mutable nothing happen. */ - virtual void addAtIndex(int index, T &&value) { - T val = value; - addAtIndex(index, val); + virtual void addAtIndex(const int index, T &&value) { + if (this->isMutable()) { + return;// Mutable lists cannot save rvalues! + } + addAtIndex(index, value); } +#endif /*! - * @brief Add all entries from the given list to this list at the given index. - * The original entry at this index, and followings, will be placed + * @brief Add all entries from the given list to this list at a specified + * index. The original entry at this index, and followings, will be placed * directly after the entries of the given list. + * @note The elements from the other list, remain untouched. + * @note If the other list is mutable and the values saved in the lists are primitives, nothing happens. * - * @param index Index, at which the list should be added. - * @param list List to add. + * @param index Index of this list, at which all entries should be added. + * @param list Other list from where to copy the entries. */ void addAll(int index, AbstractList &list) { for (int i = 0; i < list.getSize(); i++) { - T val = list.getValue(i); - T *finalValue; - createFinalValue(val, finalValue, T); - addAtIndex(index++, *finalValue); - if (!this->isMutable()) { - free(finalValue); - } + addAtIndex(index++, list.get(i)); } } /*! * @brief Add all entries from the given list at the end of the list. + * @note The elements from the other list, remain untouched. + * @note If the other list is mutable and the values saved in the lists are primitives, nothing happens. * * @param list Other list to copy from. */ void addAll(AbstractList &list) { addAll(getSize(), list); } /*! - * @brief Add all entries from the given array + * @brief Add all entries from an array to this list at a specified index. + * The original entry at this index, and followings, will be placed + * directly after the entries of the given list. + * @note The elements from the other list, remain untouched. + * @note If this list is mutable, ensure, that the other lists do not go out-of-scope during all operations of this list. If the other list is mutable, all values added to this lists are immutable. + * + * @param index Index of this list, at which all entries should be added. + * @param arr Array. + * @param arrSize Size of the array. + */ + void addAll(int index, T *arr, const size_t arrSize) { + for (size_t i = 0; i < arrSize; ++i) { + addAtIndex(index++, arr[i]); + } + } + + /*! + * @brief Add all entries from an array. + * @note The elements from the other list, remain untouched. * - * @param arr Array - * @param size Size of array + * @param arr Array. + * @param arrSize Size of array. */ - void addAll(T *arr, size_t size) { - for (size_t i = 0; i < size; ++i) { + void addAll(T *arr, const size_t arrSize) { + for (size_t i = 0; i < arrSize; ++i) { add(arr[i]); } } /*! * @brief Add a new entry at the beginning of the list. + * @note If this list is mutable, ensure, that all variables added to the lists do not go out-of-scope during all operations of the list. * * @param value Value to add. */ void addFirst(T &value) { addAtIndex(0, value); } +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) /*! * @copydoc AbstractList::addFirst() + * @note If the list is mutable and the values saved in the list are not primitives, nothing happen. */ - void addFirst(T value) { addAtIndex(0, value); } + void addFirst(T &&value) { addAtIndex(0, value); } +#endif /*! * @brief Add a new entry at the end of the list. + * @note If this list is mutable, ensure, that all variables added to the lists do not go out-of-scope during all operations of the list. * * @param value Value to add. */ void addLast(T &value) { addAtIndex(getSize(), value); } +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) /*! * @copydoc AbstractList::addLast() + * @note If the list is mutable and the values saved in the list are not primitives, nothing happen. */ void addLast(T &&value) { addAtIndex(getSize(), value); } +#endif /*! - * @copydoc AbstractList::get() + * @brief Get the raw value at a specified index. + * + * @note Allowed indices are 0 to getSize() - 1. If the index is out of + * bounds, undefined behaviour will happen. Please ne sure, that the index is + * valid! + * @note This will method will always return an immutable object. If you want + * to get the mutable object from your mutable list, please use + * getMutableValue(). + * + * @param index Index of the element to get. + * @return Immutable object. */ - T *getPointer(int index) { return get(index); } + T get(const int index) { return *this->getPointer(index); } /*! - * @brief Get the value at the index. - * @note Be safe, that the index exists otherwise the program will crash - * here! + * @brief Get the pointer to the mutable object at a specified index. * - * @param index Index of element to get. - * @return Value. - */ - T getValue(int index) { - T *ptr = getPointer(index); - T val = *ptr; - if (!isMutable()) { - free(ptr); + * @note Allowed indices are 0 to getSize() -1. If the index is out of bounds, + * a nullptr will be returned. + * @note This method will only return a valid object for a mutable list. A + * immutable list will return always the nullptr. + * + * @param index Index of teh element to get. + * @return Mutable object, or nullptr if the index is out of bounds or the + * list is immutable. + */ + T *getMutableValue(const int index) { + if (!this->isMutable()) { + return nullptr; } - return val; + + return this->getPointer(index); } /*! @@ -256,6 +300,9 @@ template class AbstractList { /*! * @brief Remove the entry at the given index. * + * @note Allowed indices are 0 to getSize() - 1. If the index is out of + * bounds, nothing will happen. + * * @param index Index of element to remove. */ virtual void remove(int index) = 0; @@ -271,7 +318,7 @@ template class AbstractList { virtual void removeLast() { remove(getSize() - 1); } /*! - * @copybrief AbstractList::clear() + * @copydoc AbstractList::clear() * @note Alias of clear(). * @see clear() */ @@ -282,68 +329,21 @@ template class AbstractList { * * @return Size of the list. */ - int getSize() { return size; } + int getSize() const { return size; } /*! * @brief Check if the list is mutable. * * @return true if the list is mutable; false otherwise. */ - bool isMutable() { return mutableList; } + bool isMutable() const { return mutableList; } /*! * @brief Check if the list is empty. * * @return true if the list is empty; false otherwise */ - bool isEmpty() { return getSize() == 0; } - - /*! - * @brief Get an array which represent the list. - * @note The returned pointer has to be free'd with free() in order to - * prevent memory leaks. - * - * @return Array representation of the list or null if the list is empty. - */ - T *toArray() { - if (getSize() == 0) { - return nullptr; - } - - T *arr = (T *)malloc(getSize() * sizeof(T)); - - for (int i = 0; i < getSize(); ++i) { - arr[i] = getValue(i); - } - - return arr; - } - - /*! - * @brief Create the list from given array. - * @note Removes all entries in current list. - * - * @param arr Array - * @param size Size of Array - */ - void fromArray(T *arr, size_t size) { - this->clear(); - addAll(arr, size); - } - - /*! - * @brief Sort the entries in the list with Quicksort. - * - * @param compFunc Comparator Method - */ - void sort(int (*compFunc)(const void *, const void *)) { - T *arr = this->toArray(); - - qsort(arr, getSize(), sizeof(*arr), compFunc); - - this->fromArray(arr, getSize()); - free(arr); - } + bool isEmpty() const { return getSize() == 0; } /*! * @brief Compare two lists whether their attributes and entries are equal. @@ -354,27 +354,33 @@ template class AbstractList { * @return true if the lists are equal; false otherwise. */ bool equals(AbstractList &other) { - if (other.isMutable() != isMutable()) { + if (other.isMutable() != this->isMutable()) { return false; } - if (other.getSize() != getSize()) { + if (other.getSize() != this->getSize()) { return false; } for (int i = 0; i < getSize(); i++) { - if (other.getValue(i) != getValue(i)) { - return false; + if (this->isMutable()) { + if (other.getMutableValue(i) != this->getMutableValue(i)) { + return false; + } + } else { + if (other.get(i) != this->get(i)) { + return false; + } } } return true; } /*! - * @copydoc AbstractList::getValue() - * @see getValue() + * @copydoc AbstractList::get() + * @see get() */ - T operator[](int index) { return getValue(index); } + T operator[](const int index) { return get(index); } /*! * @copydoc AbstractList::equals() @@ -389,25 +395,30 @@ template class AbstractList { * @param other Other list to compare * @return true if the lists are not equal; false otherwise. */ - bool operator!=(AbstractList &other) { return !(*this == other); } + bool operator!=(AbstractList &other) { return !this->equals(other); } /*! * @copydoc AbstractList::add() + * @note If this list is mutable, ensure, that all variables added to the lists do not go out-of-scope during all operations of the list. * @see add() */ void operator+(T &value) { this->add(value); } +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) /*! * @copydoc AbstractList::add() * @see add() + * @note If the list is mutable and the values saved in the list are not primitives, nothing happen. */ void operator+(T &&value) { this->add(value); } +#endif /*! * @copydoc AbstractList::addAll(AbstractList&) + * @note If the other list is mutable and the values saved in the lists are primitives, nothing happens. * @see addAll(AbstractList&) */ void operator+(AbstractList &list) { this->addAll(list); } }; -#endif // LIST_ABSTRACT_LIST_HPP +#endif// LIST_ABSTRACT_LIST_HPP diff --git a/src/DoubleLinkedList.hpp b/src/DoubleLinkedList.hpp index ea888d0..309b5c3 100644 --- a/src/DoubleLinkedList.hpp +++ b/src/DoubleLinkedList.hpp @@ -5,7 +5,7 @@ * easy-to-use list implementations. They are specially designed and optimized * for different purposes. * - * Copyright (C) 2022 Niklas Kaaf + * Copyright (C) 2022-2023 Niklas Kaaf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -33,24 +33,16 @@ * * @tparam T Data Type of entries, that should be stored in the list. */ -template class DoubleLinkedList : public AbstractList { -private: +template +class DoubleLinkedList : public AbstractList { /*! * @brief Class representing one entry of the list. */ class Entry : public AbstractList::AbstractEntry { - private: - Entry *prev = nullptr; /// Pointer to the previous element of the list. - Entry *next = nullptr; /// Pointer to the next element of the list. - - public: - /*! - * @brief Constructor of an Entry Object. - * - * @param value Value of the entry. - */ - explicit Entry(T *value) : AbstractList::AbstractEntry(value) {} + Entry *prev = nullptr;/// Pointer to the previous element of the list. + Entry *next = nullptr;/// Pointer to the next element of the list. + public: /*! * @brief Destructor of an Entry Object. */ @@ -88,14 +80,14 @@ template class DoubleLinkedList : public AbstractList { void setPrev(Entry *prevEntry) { prev = prevEntry; } }; - Entry *head = nullptr; /// The first entry of the list. - Entry *tail = nullptr; /// The last entry of the list. + Entry *head = nullptr;/// The first entry of the list. + Entry *tail = nullptr;/// The last entry of the list. -protected: + protected: /*! - * @copydoc AbstractList::get() + * @copydoc AbstractList::getPointer() */ - T *get(int index) override { + T *getPointer(int index) override { if (this->isIndexOutOfBounds(index)) { return nullptr; } @@ -114,17 +106,10 @@ template class DoubleLinkedList : public AbstractList { } } - if (this->isMutable()) { - return (T *)current->getValue(); - } else { - T *val = current->getValue(); - T *finalValue; - createFinalValue(*val, finalValue, T); - return finalValue; - } + return current->getValue(this->isMutable()); } -public: + public: /*! * @brief Constructor of a DoubleLinkedList Object. * @@ -139,6 +124,10 @@ template class DoubleLinkedList : public AbstractList { */ ~DoubleLinkedList() { this->clear(); } + using AbstractList::addAtIndex;///'Using' the addAtIndex method, to + /// prevent name hiding of the addAtIndex + /// method from AbstractList + /*! * @copydoc AbstractList::addAtIndex() */ @@ -149,15 +138,8 @@ template class DoubleLinkedList : public AbstractList { return; } - Entry *entry; - - if (this->isMutable()) { - entry = new Entry(&value); - } else { - T *finalValue; - createFinalValue(value, finalValue, T); - entry = new Entry(finalValue); - } + Entry *entry = new Entry(); + entry->setValue(value, this->isMutable()); if (index == 0) { if (this->getSize() == 0) { @@ -186,6 +168,7 @@ template class DoubleLinkedList : public AbstractList { } entry->setNext(current); entry->setPrev(current->getPrev()); + entry->getPrev()->setNext(entry); current->setPrev(entry); } else { current = head; @@ -206,19 +189,17 @@ template class DoubleLinkedList : public AbstractList { * @copydoc AbstractList::clear() */ void clear() override { - if (this->getSize() > 0) { - Entry *current = head; - Entry *next; - for (int i = 0; i < this->getSize(); ++i) { - next = current->getNext(); - - if (!this->isMutable()) { - current->freeValue(); - } + if (this->getSize() == 0) { + return; + } - delete current; - current = next; - } + Entry *current = head; + Entry *next; + for (int i = 0; i < this->getSize(); ++i) { + next = current->getNext(); + + delete current; + current = next; } this->resetSize(); @@ -243,7 +224,7 @@ template class DoubleLinkedList : public AbstractList { int i = this->getSize() - 1; while (i > index - 1) { current = current->getPrev(); - i--; + --i; } } } else { @@ -276,9 +257,6 @@ template class DoubleLinkedList : public AbstractList { } } - if (!this->isMutable()) { - toDelete->freeValue(); - } delete toDelete; this->decreaseSize(); @@ -290,4 +268,4 @@ template class DoubleLinkedList : public AbstractList { } }; -#endif // LIST_DOUBLE_LINKED_LIST_HPP +#endif// LIST_DOUBLE_LINKED_LIST_HPP diff --git a/src/List.hpp b/src/List.hpp index 056f3ef..f3e7b2f 100644 --- a/src/List.hpp +++ b/src/List.hpp @@ -22,7 +22,7 @@ * * This file is part of the List library. * - * Copyright (C) 2021-2022 Niklas Kaaf + * Copyright (C) 2021-2023 Niklas Kaaf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -50,16 +50,19 @@ * * @tparam T Data Type of entries, that should be stored in the list. */ -template class List : public SingleLinkedList { -public: +template +class List : public SingleLinkedList { + public: /*! * @brief Constructor of a List Object. * * @param mutableList true if the list should be mutable; false otherwise * (default). + * @note Mutable lists only work as expected, if the values, that are added, are + * only lvalues and you can ensure, that the variables do not go out-of-scope during all operations of the list. */ explicit List(bool mutableList = false) : SingleLinkedList(mutableList) {} }; -#endif // LIST_HPP +#endif// LIST_HPP diff --git a/src/SingleLinkedList.hpp b/src/SingleLinkedList.hpp index 75ab2a5..5b0c98f 100644 --- a/src/SingleLinkedList.hpp +++ b/src/SingleLinkedList.hpp @@ -5,7 +5,7 @@ * easy-to-use list implementations. They are specially designed and optimized * for different purposes. * - * Copyright (C) 2021-2022 Niklas Kaaf + * Copyright (C) 2021-2023 Niklas Kaaf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -33,23 +33,15 @@ * * @tparam T Data Type of entries, that should be stored in the list. */ -template class SingleLinkedList : public AbstractList { -private: +template +class SingleLinkedList : public AbstractList { /*! * @brief Class representing one entry of the list. */ class Entry : public AbstractList::AbstractEntry { - private: - Entry *next = nullptr; /// Pointer to the next element of the list - - public: - /*! - * @brief Constructor of an Entry Object. - * - * @param value Value of the entry. - */ - explicit Entry(T *value) : AbstractList::AbstractEntry(value) {} + Entry *next = nullptr;/// Pointer to the next element of the list + public: /*! * @brief Destructor of an Entry Object. */ @@ -70,14 +62,14 @@ template class SingleLinkedList : public AbstractList { void setNext(Entry *nextEntry) { next = nextEntry; } }; - Entry *head = nullptr; /// The first entry of the list. - Entry *tail = nullptr; /// The last entry of the list. + Entry *head = nullptr;/// The first entry of the list. + Entry *tail = nullptr;/// The last entry of the list. -protected: + protected: /*! - * @copydoc AbstractList::get() + * @copydoc AbstractList::getPointer() */ - T *get(int index) override { + T *getPointer(int index) override { if (this->isIndexOutOfBounds(index)) { return nullptr; } @@ -88,18 +80,10 @@ template class SingleLinkedList : public AbstractList { current = current->getNext(); i++; } - - if (this->isMutable()) { - return (T *)current->getValue(); - } else { - T *val = current->getValue(); - T *finalValue; - createFinalValue(*val, finalValue, T); - return finalValue; - } + return current->getValue(this->isMutable()); } -public: + public: /*! * @brief Constructor of a SingleLinkedList Object. * @@ -114,6 +98,10 @@ template class SingleLinkedList : public AbstractList { */ ~SingleLinkedList() { this->clear(); } + using AbstractList::addAtIndex;///'Using' the addAtIndex method, to + /// prevent name hiding of the addAtIndex + /// method from AbstractList + /*! * @copydoc AbstractList::addAtIndex() */ @@ -126,13 +114,8 @@ template class SingleLinkedList : public AbstractList { Entry *entry; - if (this->isMutable()) { - entry = new Entry(&value); - } else { - T *finalValue; - createFinalValue(value, finalValue, T); - entry = new Entry(finalValue); - } + entry = new Entry(); + entry->setValue(value, this->isMutable()); if (index == 0) { if (this->getSize() == 0) { @@ -164,24 +147,22 @@ template class SingleLinkedList : public AbstractList { * @copydoc AbstractList::clear() */ void clear() override { - if (this->getSize() > 0) { - Entry *current = head; - Entry *next; - for (int i = 0; i < this->getSize(); ++i) { - next = current->getNext(); - - if (!this->isMutable()) { - current->freeValue(); - } + if (this->getSize() == 0) { + return; + } - delete current; - current = next; - } + Entry *current = head; + Entry *next; + for (int i = 0; i < this->getSize(); ++i) { + next = current->getNext(); - this->resetSize(); - head = nullptr; - tail = nullptr; + delete current; + current = next; } + + this->resetSize(); + head = nullptr; + tail = nullptr; } /*! @@ -216,9 +197,6 @@ template class SingleLinkedList : public AbstractList { current->setNext(current->getNext()->getNext()); } - if (!this->isMutable()) { - toDelete->freeValue(); - } delete toDelete; this->decreaseSize(); @@ -230,4 +208,4 @@ template class SingleLinkedList : public AbstractList { } }; -#endif // LIST_SINGLE_LINKED_LIST_HPP +#endif// LIST_SINGLE_LINKED_LIST_HPP diff --git a/test/test_immutable_doublelinkedlist_+/test_immutable_doublelinkedlist_+.cpp b/test/test_immutable_doublelinkedlist_+/test_immutable_doublelinkedlist_+.cpp new file mode 100644 index 0000000..443c956 --- /dev/null +++ b/test/test_immutable_doublelinkedlist_+/test_immutable_doublelinkedlist_+.cpp @@ -0,0 +1,246 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void add_ds_rvalue_primitive(void) { + DoubleLinkedList list; + + { + list + 1; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list + 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_primitive(void) { + DoubleLinkedList list; + + { + int a = 1; + list + a; + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list + b; + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_rvalue_class(void) { + DoubleLinkedList list; + + { + list + "1"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list + "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_class(void) { + DoubleLinkedList list; + + { + String a = "1"; + list + a; + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list + b; + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void add_ss_rvalue_primitive(void) { + DoubleLinkedList list; + + list + 1; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_primitive(void) { + DoubleLinkedList list; + + int a = 1, b = 2; + + list + a; + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + b; + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_rvalue_class(void) { + DoubleLinkedList list; + + list + "1"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_class(void) { + DoubleLinkedList list; + + String a = "1", b = "2"; + + list + a; + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + b; + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void operator+(AbstractList &list) ---------- // + +void addAll_primitive(void) { + DoubleLinkedList fromList; + DoubleLinkedList toList; + + toList.add(1); + toList.add(2); + toList.add(3); + fromList.add(4); + fromList.add(5); + fromList.add(6); + + toList + fromList; + + TEST_ASSERT_EQUAL_INT(4, fromList[0]); + TEST_ASSERT_EQUAL_INT(5, fromList[1]); + TEST_ASSERT_EQUAL_INT(6, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_class(void) { + DoubleLinkedList fromList; + DoubleLinkedList toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + fromList.add("4"); + fromList.add("5"); + fromList.add("6"); + + toList + fromList; + + TEST_ASSERT_EQUAL_STRING("4", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(add_ds_rvalue_primitive); + RUN_TEST(add_ds_lvalue_primitive); + RUN_TEST(add_ds_rvalue_class); + RUN_TEST(add_ds_lvalue_class); + + // same scope tests + RUN_TEST(add_ss_rvalue_primitive); + RUN_TEST(add_ss_lvalue_primitive); + RUN_TEST(add_ss_rvalue_class); + RUN_TEST(add_ss_lvalue_class); + + // ---------- void operator+(AbstractList &list) ---------- // + RUN_TEST(addAll_primitive); + RUN_TEST(addAll_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_add/test_immutable_doublelinkedlist_add.cpp b/test/test_immutable_doublelinkedlist_add/test_immutable_doublelinkedlist_add.cpp new file mode 100644 index 0000000..20691a7 --- /dev/null +++ b/test/test_immutable_doublelinkedlist_add/test_immutable_doublelinkedlist_add.cpp @@ -0,0 +1,184 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void add_ds_rvalue_primitive(void) { + DoubleLinkedList list; + + { + list.add(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.add(2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_primitive(void) { + DoubleLinkedList list; + + { + int a = 1; + list.add(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.add(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_rvalue_class(void) { + DoubleLinkedList list; + + { + list.add("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.add("2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_class(void) { + DoubleLinkedList list; + + { + String a = "1"; + list.add(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.add(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void add_ss_rvalue_primitive(void) { + DoubleLinkedList list; + + list.add(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_primitive(void) { + DoubleLinkedList list; + + int a = 1, b = 2; + + list.add(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_rvalue_class(void) { + DoubleLinkedList list; + + list.add("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_class(void) { + DoubleLinkedList list; + + String a = "1", b = "2"; + + list.add(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(add_ds_rvalue_primitive); + RUN_TEST(add_ds_lvalue_primitive); + RUN_TEST(add_ds_rvalue_class); + RUN_TEST(add_ds_lvalue_class); + + // same scope tests + RUN_TEST(add_ss_rvalue_primitive); + RUN_TEST(add_ss_lvalue_primitive); + RUN_TEST(add_ss_rvalue_class); + RUN_TEST(add_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_addAll/test_immutable_doublelinkedlist_addAll.cpp b/test/test_immutable_doublelinkedlist_addAll/test_immutable_doublelinkedlist_addAll.cpp new file mode 100644 index 0000000..884cc2b --- /dev/null +++ b/test/test_immutable_doublelinkedlist_addAll/test_immutable_doublelinkedlist_addAll.cpp @@ -0,0 +1,262 @@ +#include + +#include "unity.h" + +#include + +// ---------- void addAll(AbstractList &list) ---------- // + +void addAll_list_primitive(void) { + DoubleLinkedList fromList; + DoubleLinkedList toList; + + toList.add(1); + toList.add(2); + toList.add(3); + fromList.add(4); + fromList.add(5); + fromList.add(6); + + toList.addAll(fromList); + + TEST_ASSERT_EQUAL_INT(4, fromList[0]); + TEST_ASSERT_EQUAL_INT(5, fromList[1]); + TEST_ASSERT_EQUAL_INT(6, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_list_class(void) { + DoubleLinkedList fromList; + DoubleLinkedList toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + fromList.add("4"); + fromList.add("5"); + fromList.add("6"); + + toList.addAll(fromList); + + TEST_ASSERT_EQUAL_STRING("4", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(int index, AbstractList &list) ---------- // + +void addAll_index_primitive(void) { + DoubleLinkedList fromList; + DoubleLinkedList toList; + + toList.add(1); + toList.add(5); + toList.add(6); + fromList.add(2); + fromList.add(3); + fromList.add(4); + + toList.addAll(1, fromList); + + TEST_ASSERT_EQUAL_INT(2, fromList[0]); + TEST_ASSERT_EQUAL_INT(3, fromList[1]); + TEST_ASSERT_EQUAL_INT(4, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_index_class(void) { + DoubleLinkedList fromList; + DoubleLinkedList toList; + + toList.add("1"); + toList.add("5"); + toList.add("6"); + fromList.add("2"); + fromList.add("3"); + fromList.add("4"); + + toList.addAll(1, fromList); + + TEST_ASSERT_EQUAL_STRING("2", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("4", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(T *arr, size_t arrSize) ---------- // + +void addAll_array_primitive(void) { + int fromArray[] = {4, 5, 6}; + DoubleLinkedList toList; + + toList.add(1); + toList.add(2); + toList.add(3); + + toList.addAll(fromArray, 3); + + TEST_ASSERT_EQUAL_INT(4, fromArray[0]); + TEST_ASSERT_EQUAL_INT(5, fromArray[1]); + TEST_ASSERT_EQUAL_INT(6, fromArray[2]); + + fromArray[0] = 7; + fromArray[1] = 8; + fromArray[2] = 9; + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_array_class(void) { + String fromArray[] = {"4", "5", "6"}; + DoubleLinkedList toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + + toList.addAll(fromArray, 3); + + TEST_ASSERT_EQUAL_STRING("4", fromArray[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromArray[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromArray[2].c_str()); + + fromArray[0] = "7"; + fromArray[1] = "8"; + fromArray[2] = "9"; + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(int index, T* arr, size_t arrSize) ---------- // + +void addAll_array_index_primitive(void) { + int fromArray[] = {2, 3, 4}; + DoubleLinkedList toList; + + toList.add(1); + toList.add(5); + toList.add(6); + + toList.addAll(1, fromArray, 3); + + TEST_ASSERT_EQUAL_INT(2, fromArray[0]); + TEST_ASSERT_EQUAL_INT(3, fromArray[1]); + TEST_ASSERT_EQUAL_INT(4, fromArray[2]); + + fromArray[0] = 7; + fromArray[1] = 8; + fromArray[2] = 9; + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_array_index_class(void) { + String fromArray[] = {"2", "3", "4"}; + DoubleLinkedList toList; + + toList.add("1"); + toList.add("5"); + toList.add("6"); + + toList.addAll(1, fromArray, 3); + + TEST_ASSERT_EQUAL_STRING("2", fromArray[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", fromArray[1].c_str()); + TEST_ASSERT_EQUAL_STRING("4", fromArray[2].c_str()); + + fromArray[0] = "7"; + fromArray[1] = "8"; + fromArray[2] = "9"; + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- void addAll(AbstractList &list) ---------- // + RUN_TEST(addAll_list_primitive); + RUN_TEST(addAll_list_class); + + // ---------- void addAll(int index, AbstractList &list) ---------- // + RUN_TEST(addAll_index_primitive); + RUN_TEST(addAll_index_class); + + // ---------- void addAll(T *arr, size_t arrSize) ---------- // + RUN_TEST(addAll_array_primitive); + RUN_TEST(addAll_array_class); + + // ---------- void addAll(int index, T* arr, size_t arrSize) ---------- // + RUN_TEST(addAll_array_index_primitive); + RUN_TEST(addAll_array_index_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_addAtIndex/test_immutable_doublelinkedlist_addAtIndex.cpp b/test/test_immutable_doublelinkedlist_addAtIndex/test_immutable_doublelinkedlist_addAtIndex.cpp new file mode 100644 index 0000000..edec0a7 --- /dev/null +++ b/test/test_immutable_doublelinkedlist_addAtIndex/test_immutable_doublelinkedlist_addAtIndex.cpp @@ -0,0 +1,252 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addAtIndex_ds_rvalue_primitive(void) { + DoubleLinkedList list; + + { + list.addAtIndex(0, 1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addAtIndex(1, 2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + list.addAtIndex(1, 3); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_lvalue_primitive(void) { + DoubleLinkedList list; + + { + int a = 1; + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + int c = 3; + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_INT(3, c); + c = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_rvalue_class(void) { + DoubleLinkedList list; + + { + list.addAtIndex(0, "1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addAtIndex(1, "2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + list.addAtIndex(1, "3"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_lvalue_class(void) { + DoubleLinkedList list; + + { + String a = "1"; + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + String c = "3"; + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_STRING("3", c.c_str()); + c = "4"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addAtIndex_ss_rvalue_primitive(void) { + DoubleLinkedList list; + + list.addAtIndex(0, 1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, 2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, 3); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_lvalue_primitive(void) { + DoubleLinkedList list; + + int a = 1, b = 2, c = 3; + + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_INT(3, c); + c = 4; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_rvalue_class(void) { + DoubleLinkedList list; + + list.addAtIndex(0, "1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, "2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, "3"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_lvalue_class(void) { + DoubleLinkedList list; + + String a = "1", b = "2", c = "3"; + + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_STRING("3", c.c_str()); + c = "4"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addAtIndex_ds_rvalue_primitive); + RUN_TEST(addAtIndex_ds_lvalue_primitive); + RUN_TEST(addAtIndex_ds_rvalue_class); + RUN_TEST(addAtIndex_ds_lvalue_class); + + // same scope tests + RUN_TEST(addAtIndex_ss_rvalue_primitive); + RUN_TEST(addAtIndex_ss_lvalue_primitive); + RUN_TEST(addAtIndex_ss_rvalue_class); + RUN_TEST(addAtIndex_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_addFirst/test_immutable_doublelinkedlist_addFirst.cpp b/test/test_immutable_doublelinkedlist_addFirst/test_immutable_doublelinkedlist_addFirst.cpp new file mode 100644 index 0000000..2a26681 --- /dev/null +++ b/test/test_immutable_doublelinkedlist_addFirst/test_immutable_doublelinkedlist_addFirst.cpp @@ -0,0 +1,182 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addFirst_ds_rvalue_primitive(void) { + DoubleLinkedList list; + + { + list.addFirst(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addFirst(2); + } + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_lvalue_primitive(void) { + DoubleLinkedList list; + + { + int a = 1; + list.addFirst(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addFirst(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_rvalue_class(void) { + DoubleLinkedList list; + + { + list.addFirst("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addFirst("2"); + } + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_lvalue_class(void) { + DoubleLinkedList list; + + { + String a = "1"; + list.addFirst(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addFirst(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addFirst_ss_rvalue_primitive(void) { + DoubleLinkedList list; + + list.addFirst(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(2); + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_lvalue_primitive(void) { + DoubleLinkedList list; + + int a = 1, b = 2; + + list.addFirst(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_rvalue_class(void) { + DoubleLinkedList list; + + list.addFirst("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst("2"); + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_lvalue_class(void) { + DoubleLinkedList list; + + String a = "1", b = "2"; + + list.addFirst(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addFirst_ds_rvalue_primitive); + RUN_TEST(addFirst_ds_lvalue_primitive); + RUN_TEST(addFirst_ds_rvalue_class); + RUN_TEST(addFirst_ds_lvalue_class); + + // same scope tests + RUN_TEST(addFirst_ss_rvalue_primitive); + RUN_TEST(addFirst_ss_lvalue_primitive); + RUN_TEST(addFirst_ss_rvalue_class); + RUN_TEST(addFirst_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_addLast/test:immutable_doublelinkedlist_addLast.cpp b/test/test_immutable_doublelinkedlist_addLast/test:immutable_doublelinkedlist_addLast.cpp new file mode 100644 index 0000000..f665a0b --- /dev/null +++ b/test/test_immutable_doublelinkedlist_addLast/test:immutable_doublelinkedlist_addLast.cpp @@ -0,0 +1,182 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addLast_ds_rvalue_primitive(void) { + DoubleLinkedList list; + + { + list.addLast(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addLast(2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_lvalue_primitive(void) { + DoubleLinkedList list; + + { + int a = 1; + list.addLast(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addLast(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_rvalue_class(void) { + DoubleLinkedList list; + + { + list.addLast("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addLast("2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_lvalue_class(void) { + DoubleLinkedList list; + + { + String a = "1"; + list.addLast(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addLast(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addLast_ss_rvalue_primitive(void) { + DoubleLinkedList list; + + list.addLast(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_lvalue_primitive(void) { + DoubleLinkedList list; + + int a = 1, b = 2; + + list.addLast(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_rvalue_class(void) { + DoubleLinkedList list; + + list.addLast("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast("2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_lvalue_class(void) { + DoubleLinkedList list; + + String a = "1", b = "2"; + + list.addLast(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addLast_ds_rvalue_primitive); + RUN_TEST(addLast_ds_lvalue_primitive); + RUN_TEST(addLast_ds_rvalue_class); + RUN_TEST(addLast_ds_lvalue_class); + + // same scope tests + RUN_TEST(addLast_ss_rvalue_primitive); + RUN_TEST(addLast_ss_lvalue_primitive); + RUN_TEST(addLast_ss_rvalue_class); + RUN_TEST(addLast_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_diverse/test_immutable_doublelinkedlist_diverse.cpp b/test/test_immutable_doublelinkedlist_diverse/test_immutable_doublelinkedlist_diverse.cpp new file mode 100644 index 0000000..c9e0979 --- /dev/null +++ b/test/test_immutable_doublelinkedlist_diverse/test_immutable_doublelinkedlist_diverse.cpp @@ -0,0 +1,112 @@ +#include + +#include "unity.h" + +#include + +// ---------- getSize ---------- // + +void getSize_primitive(void) { + DoubleLinkedList list; + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void getSize_class(void) { + DoubleLinkedList list; + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +// ---------- is mutable ---------- // + +void isMutable_primitive(void) { + DoubleLinkedList list; + + TEST_ASSERT_FALSE(list.isMutable()); +} + +void isMutable_class(void) { + DoubleLinkedList list; + + TEST_ASSERT_FALSE(list.isMutable()); +} + +// ---------- is empty ---------- // + +void isEmpty_primitive(void) { + DoubleLinkedList list; + + TEST_ASSERT_TRUE(list.isEmpty()); + + list.add(1); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.add(2); + list.add(3); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.clear(); + + TEST_ASSERT_TRUE(list.isEmpty()); +} + +void isEmpty_class(void) { + DoubleLinkedList list; + + TEST_ASSERT_TRUE(list.isEmpty()); + + list.add("1"); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.add("2"); + list.add("3"); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.clear(); + + TEST_ASSERT_TRUE(list.isEmpty()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- getSize ---------- // + RUN_TEST(getSize_primitive); + RUN_TEST(getSize_class); + + // ---------- is mutable ---------- // + RUN_TEST(isMutable_primitive); + RUN_TEST(isMutable_class); + + // ---------- is empty ---------- // + RUN_TEST(isEmpty_primitive); + RUN_TEST(isEmpty_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_equality/test_immutable_doublelinkedlist_equality.cpp b/test/test_immutable_doublelinkedlist_equality/test_immutable_doublelinkedlist_equality.cpp new file mode 100644 index 0000000..90fab4f --- /dev/null +++ b/test/test_immutable_doublelinkedlist_equality/test_immutable_doublelinkedlist_equality.cpp @@ -0,0 +1,216 @@ +#include + +#include "unity.h" + +#include + +// ---------- bool equals(AbstractList &other) ---------- // + +void equals_primitive(void) { + DoubleLinkedList list; + DoubleLinkedList other; + + TEST_ASSERT_TRUE(list.equals(other)); + + list.add(1); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_TRUE(list.equals(other)); + + other.add(3); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add(3); + + TEST_ASSERT_TRUE(list.equals(other)); + + list.clear(); + + DoubleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list.equals(mutableList)); +} + +void equals_class(void) { + DoubleLinkedList list; + DoubleLinkedList other; + + TEST_ASSERT_TRUE(list.equals(other)); + + list.add("1"); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_TRUE(list.equals(other)); + + other.add("3"); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add("3"); + + TEST_ASSERT_TRUE(list.equals(other)); + + list.clear(); + + DoubleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list.equals(mutableList)); +} + +// ---------- bool operator==(AbstractList &other) ---------- // +void equals_operator_primitive(void) { + DoubleLinkedList list; + DoubleLinkedList other; + + TEST_ASSERT_TRUE(list == other); + + list.add(1); + + TEST_ASSERT_FALSE(list == other); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_TRUE(list == other); + + other.add(3); + + TEST_ASSERT_FALSE(list == other); + + list.add(3); + + TEST_ASSERT_TRUE(list == other); + + list.clear(); + + DoubleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list == mutableList); +} + +void equals_operator_class(void) { + DoubleLinkedList list; + DoubleLinkedList other; + + TEST_ASSERT_TRUE(list == other); + + list.add("1"); + + TEST_ASSERT_FALSE(list == other); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_TRUE(list == other); + + other.add("3"); + + TEST_ASSERT_FALSE(list == other); + + list.add("3"); + + TEST_ASSERT_TRUE(list == other); + + list.clear(); + + DoubleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list == mutableList); +} + +// ---------- bool operator!=(AbstractList &other) ---------- // +void not_equals_operator_primitive(void) { + DoubleLinkedList list; + DoubleLinkedList other; + + TEST_ASSERT_FALSE(list != other); + + list.add(1); + + TEST_ASSERT_TRUE(list != other); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_FALSE(list != other); + + other.add(3); + + TEST_ASSERT_TRUE(list != other); + + list.add(3); + + TEST_ASSERT_FALSE(list != other); + + list.clear(); + + DoubleLinkedList mutableList(true); + + TEST_ASSERT_TRUE(list != mutableList); +} + +void not_equals_operator_class(void) { + DoubleLinkedList list; + DoubleLinkedList other; + + TEST_ASSERT_FALSE(list != other); + + list.add("1"); + + TEST_ASSERT_TRUE(list != other); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_FALSE(list != other); + + other.add("3"); + + TEST_ASSERT_TRUE(list != other); + + list.add("3"); + + TEST_ASSERT_FALSE(list != other); + + list.clear(); + + DoubleLinkedList mutableList(true); + + TEST_ASSERT_TRUE(list != mutableList); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- bool equals(AbstractList &other) ---------- // + RUN_TEST(equals_primitive); + RUN_TEST(equals_class); + + // ---------- bool operator==(AbstractList &other) ---------- // + RUN_TEST(equals_operator_primitive); + RUN_TEST(equals_operator_class); + + // ---------- bool operator!=(AbstractList &other) ---------- // + RUN_TEST(not_equals_operator_primitive); + RUN_TEST(not_equals_operator_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_get/test_immutable_doublelinkedlist_get.cpp b/test/test_immutable_doublelinkedlist_get/test_immutable_doublelinkedlist_get.cpp new file mode 100644 index 0000000..0883e21 --- /dev/null +++ b/test/test_immutable_doublelinkedlist_get/test_immutable_doublelinkedlist_get.cpp @@ -0,0 +1,122 @@ +#include + +#include "unity.h" + +#include + +// ---------- T get(int index) ---------- // + +void get_primitive(void) { + DoubleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.get(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_INT(1, list.get(0)); + TEST_ASSERT_EQUAL_INT(2, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void get_class(void) { + DoubleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_STRING("1", list.get(0).c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_STRING("1", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("2", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- T *getMutableValue(int index) ---------- // + +void getMutableValue_primitive(void) { + DoubleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void getMutableValue_class(void) { + DoubleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- T operator[](int index) ---------- // + +void get_operator_primitive(void) { + DoubleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void get_operator_class(void) { + DoubleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- T get(int index) ---------- // + RUN_TEST(get_primitive); + RUN_TEST(get_class); + + // ---------- T *getMutableValue(int index) ---------- // + RUN_TEST(getMutableValue_primitive); + RUN_TEST(getMutableValue_class); + + // ---------- T operator[](int index) ---------- // + RUN_TEST(get_operator_primitive); + RUN_TEST(get_operator_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_doublelinkedlist_remove/test_immutable_doublelinkedlist_remove.cpp b/test/test_immutable_doublelinkedlist_remove/test_immutable_doublelinkedlist_remove.cpp new file mode 100644 index 0000000..6732608 --- /dev/null +++ b/test/test_immutable_doublelinkedlist_remove/test_immutable_doublelinkedlist_remove.cpp @@ -0,0 +1,244 @@ +#include + +#include "unity.h" + +#include + +// ---------- void clear() ---------- // + +void clear_primitive(void) { + DoubleLinkedList list; + + list.add(1); + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.clear(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +void clear_class(void) { + DoubleLinkedList list; + + list.add("1"); + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.clear(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +// ---------- void removeAll() ---------- // + +void removeAll_primitive(void) { + DoubleLinkedList list; + + list.add(1); + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeAll(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +void removeAll_class(void) { + DoubleLinkedList list; + + list.add("1"); + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeAll(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +// ---------- void remove(int index) ---------- // + +void remove_primitive(void) { + DoubleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.remove(0); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.remove(1); + + TEST_ASSERT_EQUAL_INT(2, list.get(0)); + TEST_ASSERT_EQUAL_INT(4, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void remove_class(void) { + DoubleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.remove(0); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.remove(1); + + TEST_ASSERT_EQUAL_STRING("2", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("4", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void removeFirst() ---------- // + +void removeFirst_primitive(void) { + DoubleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(3, list.get(0)); + TEST_ASSERT_EQUAL_INT(4, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void removeFirst_class(void) { + DoubleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_STRING("3", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("4", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void removeLast() ---------- // + +void removeLast_primitive(void) { + DoubleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(2, list.get(0)); + TEST_ASSERT_EQUAL_INT(3, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void removeLast_class(void) { + DoubleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_STRING("2", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("3", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- void clear() ---------- // + RUN_TEST(clear_primitive); + RUN_TEST(clear_class); + + // ---------- void removeAll() ---------- // + RUN_TEST(removeAll_primitive); + RUN_TEST(removeAll_class); + + // ---------- void remove(int index) ---------- // + RUN_TEST(remove_primitive); + RUN_TEST(remove_class); + + // ---------- void removeFirst() ---------- // + RUN_TEST(removeFirst_primitive); + RUN_TEST(removeFirst_class); + + // ---------- void removeLast() ---------- // + RUN_TEST(removeLast_primitive); + RUN_TEST(removeLast_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_+/test_immutable_list_+.cpp b/test/test_immutable_list_+/test_immutable_list_+.cpp new file mode 100644 index 0000000..a039b94 --- /dev/null +++ b/test/test_immutable_list_+/test_immutable_list_+.cpp @@ -0,0 +1,246 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void add_ds_rvalue_primitive(void) { + List list; + + { + list + 1; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list + 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_primitive(void) { + List list; + + { + int a = 1; + list + a; + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list + b; + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_rvalue_class(void) { + List list; + + { + list + "1"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list + "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_class(void) { + List list; + + { + String a = "1"; + list + a; + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list + b; + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void add_ss_rvalue_primitive(void) { + List list; + + list + 1; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_primitive(void) { + List list; + + int a = 1, b = 2; + + list + a; + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + b; + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_rvalue_class(void) { + List list; + + list + "1"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_class(void) { + List list; + + String a = "1", b = "2"; + + list + a; + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + b; + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void operator+(AbstractList &list) ---------- // + +void addAll_primitive(void) { + List fromList; + List toList; + + toList.add(1); + toList.add(2); + toList.add(3); + fromList.add(4); + fromList.add(5); + fromList.add(6); + + toList + fromList; + + TEST_ASSERT_EQUAL_INT(4, fromList[0]); + TEST_ASSERT_EQUAL_INT(5, fromList[1]); + TEST_ASSERT_EQUAL_INT(6, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_class(void) { + List fromList; + List toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + fromList.add("4"); + fromList.add("5"); + fromList.add("6"); + + toList + fromList; + + TEST_ASSERT_EQUAL_STRING("4", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(add_ds_rvalue_primitive); + RUN_TEST(add_ds_lvalue_primitive); + RUN_TEST(add_ds_rvalue_class); + RUN_TEST(add_ds_lvalue_class); + + // same scope tests + RUN_TEST(add_ss_rvalue_primitive); + RUN_TEST(add_ss_lvalue_primitive); + RUN_TEST(add_ss_rvalue_class); + RUN_TEST(add_ss_lvalue_class); + + // ---------- void operator+(AbstractList &list) ---------- // + RUN_TEST(addAll_primitive); + RUN_TEST(addAll_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_add/test_immutable_list_add.cpp b/test/test_immutable_list_add/test_immutable_list_add.cpp new file mode 100644 index 0000000..ede2db8 --- /dev/null +++ b/test/test_immutable_list_add/test_immutable_list_add.cpp @@ -0,0 +1,184 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void add_ds_rvalue_primitive(void) { + List list; + + { + list.add(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.add(2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_primitive(void) { + List list; + + { + int a = 1; + list.add(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.add(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_rvalue_class(void) { + List list; + + { + list.add("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.add("2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_class(void) { + List list; + + { + String a = "1"; + list.add(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.add(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void add_ss_rvalue_primitive(void) { + List list; + + list.add(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_primitive(void) { + List list; + + int a = 1, b = 2; + + list.add(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_rvalue_class(void) { + List list; + + list.add("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_class(void) { + List list; + + String a = "1", b = "2"; + + list.add(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(add_ds_rvalue_primitive); + RUN_TEST(add_ds_lvalue_primitive); + RUN_TEST(add_ds_rvalue_class); + RUN_TEST(add_ds_lvalue_class); + + // same scope tests + RUN_TEST(add_ss_rvalue_primitive); + RUN_TEST(add_ss_lvalue_primitive); + RUN_TEST(add_ss_rvalue_class); + RUN_TEST(add_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_addAll/test_immutable_list_addAll.cpp b/test/test_immutable_list_addAll/test_immutable_list_addAll.cpp new file mode 100644 index 0000000..b08247c --- /dev/null +++ b/test/test_immutable_list_addAll/test_immutable_list_addAll.cpp @@ -0,0 +1,262 @@ +#include + +#include "unity.h" + +#include + +// ---------- void addAll(AbstractList &list) ---------- // + +void addAll_list_primitive(void) { + List fromList; + List toList; + + toList.add(1); + toList.add(2); + toList.add(3); + fromList.add(4); + fromList.add(5); + fromList.add(6); + + toList.addAll(fromList); + + TEST_ASSERT_EQUAL_INT(4, fromList[0]); + TEST_ASSERT_EQUAL_INT(5, fromList[1]); + TEST_ASSERT_EQUAL_INT(6, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_list_class(void) { + List fromList; + List toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + fromList.add("4"); + fromList.add("5"); + fromList.add("6"); + + toList.addAll(fromList); + + TEST_ASSERT_EQUAL_STRING("4", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(int index, AbstractList &list) ---------- // + +void addAll_index_primitive(void) { + List fromList; + List toList; + + toList.add(1); + toList.add(5); + toList.add(6); + fromList.add(2); + fromList.add(3); + fromList.add(4); + + toList.addAll(1, fromList); + + TEST_ASSERT_EQUAL_INT(2, fromList[0]); + TEST_ASSERT_EQUAL_INT(3, fromList[1]); + TEST_ASSERT_EQUAL_INT(4, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_index_class(void) { + List fromList; + List toList; + + toList.add("1"); + toList.add("5"); + toList.add("6"); + fromList.add("2"); + fromList.add("3"); + fromList.add("4"); + + toList.addAll(1, fromList); + + TEST_ASSERT_EQUAL_STRING("2", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("4", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(T *arr, size_t arrSize) ---------- // + +void addAll_array_primitive(void) { + int fromArray[] = {4, 5, 6}; + List toList; + + toList.add(1); + toList.add(2); + toList.add(3); + + toList.addAll(fromArray, 3); + + TEST_ASSERT_EQUAL_INT(4, fromArray[0]); + TEST_ASSERT_EQUAL_INT(5, fromArray[1]); + TEST_ASSERT_EQUAL_INT(6, fromArray[2]); + + fromArray[0] = 7; + fromArray[1] = 8; + fromArray[2] = 9; + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_array_class(void) { + String fromArray[] = {"4", "5", "6"}; + List toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + + toList.addAll(fromArray, 3); + + TEST_ASSERT_EQUAL_STRING("4", fromArray[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromArray[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromArray[2].c_str()); + + fromArray[0] = "7"; + fromArray[1] = "8"; + fromArray[2] = "9"; + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(int index, T* arr, size_t arrSize) ---------- // + +void addAll_array_index_primitive(void) { + int fromArray[] = {2, 3, 4}; + List toList; + + toList.add(1); + toList.add(5); + toList.add(6); + + toList.addAll(1, fromArray, 3); + + TEST_ASSERT_EQUAL_INT(2, fromArray[0]); + TEST_ASSERT_EQUAL_INT(3, fromArray[1]); + TEST_ASSERT_EQUAL_INT(4, fromArray[2]); + + fromArray[0] = 7; + fromArray[1] = 8; + fromArray[2] = 9; + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_array_index_class(void) { + String fromArray[] = {"2", "3", "4"}; + List toList; + + toList.add("1"); + toList.add("5"); + toList.add("6"); + + toList.addAll(1, fromArray, 3); + + TEST_ASSERT_EQUAL_STRING("2", fromArray[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", fromArray[1].c_str()); + TEST_ASSERT_EQUAL_STRING("4", fromArray[2].c_str()); + + fromArray[0] = "7"; + fromArray[1] = "8"; + fromArray[2] = "9"; + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- void addAll(AbstractList &list) ---------- // + RUN_TEST(addAll_list_primitive); + RUN_TEST(addAll_list_class); + + // ---------- void addAll(int index, AbstractList &list) ---------- // + RUN_TEST(addAll_index_primitive); + RUN_TEST(addAll_index_class); + + // ---------- void addAll(T *arr, size_t arrSize) ---------- // + RUN_TEST(addAll_array_primitive); + RUN_TEST(addAll_array_class); + + // ---------- void addAll(int index, T* arr, size_t arrSize) ---------- // + RUN_TEST(addAll_array_index_primitive); + RUN_TEST(addAll_array_index_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_addAtIndex/test_immutable_list_addAtIndex.cpp b/test/test_immutable_list_addAtIndex/test_immutable_list_addAtIndex.cpp new file mode 100644 index 0000000..985be8b --- /dev/null +++ b/test/test_immutable_list_addAtIndex/test_immutable_list_addAtIndex.cpp @@ -0,0 +1,252 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addAtIndex_ds_rvalue_primitive(void) { + List list; + + { + list.addAtIndex(0, 1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addAtIndex(1, 2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + list.addAtIndex(1, 3); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_lvalue_primitive(void) { + List list; + + { + int a = 1; + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + int c = 3; + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_INT(3, c); + c = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_rvalue_class(void) { + List list; + + { + list.addAtIndex(0, "1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addAtIndex(1, "2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + list.addAtIndex(1, "3"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_lvalue_class(void) { + List list; + + { + String a = "1"; + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + String c = "3"; + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_STRING("3", c.c_str()); + c = "4"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addAtIndex_ss_rvalue_primitive(void) { + List list; + + list.addAtIndex(0, 1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, 2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, 3); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_lvalue_primitive(void) { + List list; + + int a = 1, b = 2, c = 3; + + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_INT(3, c); + c = 4; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_rvalue_class(void) { + List list; + + list.addAtIndex(0, "1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, "2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, "3"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_lvalue_class(void) { + List list; + + String a = "1", b = "2", c = "3"; + + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_STRING("3", c.c_str()); + c = "4"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addAtIndex_ds_rvalue_primitive); + RUN_TEST(addAtIndex_ds_lvalue_primitive); + RUN_TEST(addAtIndex_ds_rvalue_class); + RUN_TEST(addAtIndex_ds_lvalue_class); + + // same scope tests + RUN_TEST(addAtIndex_ss_rvalue_primitive); + RUN_TEST(addAtIndex_ss_lvalue_primitive); + RUN_TEST(addAtIndex_ss_rvalue_class); + RUN_TEST(addAtIndex_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_addFirst/test_immutable_list_addFirst.cpp b/test/test_immutable_list_addFirst/test_immutable_list_addFirst.cpp new file mode 100644 index 0000000..d1b5bdd --- /dev/null +++ b/test/test_immutable_list_addFirst/test_immutable_list_addFirst.cpp @@ -0,0 +1,182 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addFirst_ds_rvalue_primitive(void) { + List list; + + { + list.addFirst(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addFirst(2); + } + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_lvalue_primitive(void) { + List list; + + { + int a = 1; + list.addFirst(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addFirst(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_rvalue_class(void) { + List list; + + { + list.addFirst("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addFirst("2"); + } + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_lvalue_class(void) { + List list; + + { + String a = "1"; + list.addFirst(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addFirst(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addFirst_ss_rvalue_primitive(void) { + List list; + + list.addFirst(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(2); + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_lvalue_primitive(void) { + List list; + + int a = 1, b = 2; + + list.addFirst(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_rvalue_class(void) { + List list; + + list.addFirst("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst("2"); + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_lvalue_class(void) { + List list; + + String a = "1", b = "2"; + + list.addFirst(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addFirst_ds_rvalue_primitive); + RUN_TEST(addFirst_ds_lvalue_primitive); + RUN_TEST(addFirst_ds_rvalue_class); + RUN_TEST(addFirst_ds_lvalue_class); + + // same scope tests + RUN_TEST(addFirst_ss_rvalue_primitive); + RUN_TEST(addFirst_ss_lvalue_primitive); + RUN_TEST(addFirst_ss_rvalue_class); + RUN_TEST(addFirst_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_addLast/test:immutable_list_addLast.cpp b/test/test_immutable_list_addLast/test:immutable_list_addLast.cpp new file mode 100644 index 0000000..1bbd516 --- /dev/null +++ b/test/test_immutable_list_addLast/test:immutable_list_addLast.cpp @@ -0,0 +1,182 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addLast_ds_rvalue_primitive(void) { + List list; + + { + list.addLast(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addLast(2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_lvalue_primitive(void) { + List list; + + { + int a = 1; + list.addLast(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addLast(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_rvalue_class(void) { + List list; + + { + list.addLast("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addLast("2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_lvalue_class(void) { + List list; + + { + String a = "1"; + list.addLast(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addLast(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addLast_ss_rvalue_primitive(void) { + List list; + + list.addLast(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_lvalue_primitive(void) { + List list; + + int a = 1, b = 2; + + list.addLast(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_rvalue_class(void) { + List list; + + list.addLast("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast("2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_lvalue_class(void) { + List list; + + String a = "1", b = "2"; + + list.addLast(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addLast_ds_rvalue_primitive); + RUN_TEST(addLast_ds_lvalue_primitive); + RUN_TEST(addLast_ds_rvalue_class); + RUN_TEST(addLast_ds_lvalue_class); + + // same scope tests + RUN_TEST(addLast_ss_rvalue_primitive); + RUN_TEST(addLast_ss_lvalue_primitive); + RUN_TEST(addLast_ss_rvalue_class); + RUN_TEST(addLast_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_diverse/test_immutable_list_diverse.cpp b/test/test_immutable_list_diverse/test_immutable_list_diverse.cpp new file mode 100644 index 0000000..cc8cfd3 --- /dev/null +++ b/test/test_immutable_list_diverse/test_immutable_list_diverse.cpp @@ -0,0 +1,112 @@ +#include + +#include "unity.h" + +#include + +// ---------- getSize ---------- // + +void getSize_primitive(void) { + List list; + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void getSize_class(void) { + List list; + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +// ---------- is mutable ---------- // + +void isMutable_primitive(void) { + List list; + + TEST_ASSERT_FALSE(list.isMutable()); +} + +void isMutable_class(void) { + List list; + + TEST_ASSERT_FALSE(list.isMutable()); +} + +// ---------- is empty ---------- // + +void isEmpty_primitive(void) { + List list; + + TEST_ASSERT_TRUE(list.isEmpty()); + + list.add(1); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.add(2); + list.add(3); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.clear(); + + TEST_ASSERT_TRUE(list.isEmpty()); +} + +void isEmpty_class(void) { + List list; + + TEST_ASSERT_TRUE(list.isEmpty()); + + list.add("1"); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.add("2"); + list.add("3"); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.clear(); + + TEST_ASSERT_TRUE(list.isEmpty()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- getSize ---------- // + RUN_TEST(getSize_primitive); + RUN_TEST(getSize_class); + + // ---------- is mutable ---------- // + RUN_TEST(isMutable_primitive); + RUN_TEST(isMutable_class); + + // ---------- is empty ---------- // + RUN_TEST(isEmpty_primitive); + RUN_TEST(isEmpty_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_equality/test_immutable_list_equality.cpp b/test/test_immutable_list_equality/test_immutable_list_equality.cpp new file mode 100644 index 0000000..c1b2274 --- /dev/null +++ b/test/test_immutable_list_equality/test_immutable_list_equality.cpp @@ -0,0 +1,216 @@ +#include + +#include "unity.h" + +#include + +// ---------- bool equals(AbstractList &other) ---------- // + +void equals_primitive(void) { + List list; + List other; + + TEST_ASSERT_TRUE(list.equals(other)); + + list.add(1); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_TRUE(list.equals(other)); + + other.add(3); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add(3); + + TEST_ASSERT_TRUE(list.equals(other)); + + list.clear(); + + List mutableList(true); + + TEST_ASSERT_FALSE(list.equals(mutableList)); +} + +void equals_class(void) { + List list; + List other; + + TEST_ASSERT_TRUE(list.equals(other)); + + list.add("1"); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_TRUE(list.equals(other)); + + other.add("3"); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add("3"); + + TEST_ASSERT_TRUE(list.equals(other)); + + list.clear(); + + List mutableList(true); + + TEST_ASSERT_FALSE(list.equals(mutableList)); +} + +// ---------- bool operator==(AbstractList &other) ---------- // +void equals_operator_primitive(void) { + List list; + List other; + + TEST_ASSERT_TRUE(list == other); + + list.add(1); + + TEST_ASSERT_FALSE(list == other); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_TRUE(list == other); + + other.add(3); + + TEST_ASSERT_FALSE(list == other); + + list.add(3); + + TEST_ASSERT_TRUE(list == other); + + list.clear(); + + List mutableList(true); + + TEST_ASSERT_FALSE(list == mutableList); +} + +void equals_operator_class(void) { + List list; + List other; + + TEST_ASSERT_TRUE(list == other); + + list.add("1"); + + TEST_ASSERT_FALSE(list == other); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_TRUE(list == other); + + other.add("3"); + + TEST_ASSERT_FALSE(list == other); + + list.add("3"); + + TEST_ASSERT_TRUE(list == other); + + list.clear(); + + List mutableList(true); + + TEST_ASSERT_FALSE(list == mutableList); +} + +// ---------- bool operator!=(AbstractList &other) ---------- // +void not_equals_operator_primitive(void) { + List list; + List other; + + TEST_ASSERT_FALSE(list != other); + + list.add(1); + + TEST_ASSERT_TRUE(list != other); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_FALSE(list != other); + + other.add(3); + + TEST_ASSERT_TRUE(list != other); + + list.add(3); + + TEST_ASSERT_FALSE(list != other); + + list.clear(); + + List mutableList(true); + + TEST_ASSERT_TRUE(list != mutableList); +} + +void not_equals_operator_class(void) { + List list; + List other; + + TEST_ASSERT_FALSE(list != other); + + list.add("1"); + + TEST_ASSERT_TRUE(list != other); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_FALSE(list != other); + + other.add("3"); + + TEST_ASSERT_TRUE(list != other); + + list.add("3"); + + TEST_ASSERT_FALSE(list != other); + + list.clear(); + + List mutableList(true); + + TEST_ASSERT_TRUE(list != mutableList); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- bool equals(AbstractList &other) ---------- // + RUN_TEST(equals_primitive); + RUN_TEST(equals_class); + + // ---------- bool operator==(AbstractList &other) ---------- // + RUN_TEST(equals_operator_primitive); + RUN_TEST(equals_operator_class); + + // ---------- bool operator!=(AbstractList &other) ---------- // + RUN_TEST(not_equals_operator_primitive); + RUN_TEST(not_equals_operator_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_get/test_immutable_list_get.cpp b/test/test_immutable_list_get/test_immutable_list_get.cpp new file mode 100644 index 0000000..3b258e1 --- /dev/null +++ b/test/test_immutable_list_get/test_immutable_list_get.cpp @@ -0,0 +1,122 @@ +#include + +#include "unity.h" + +#include + +// ---------- T get(int index) ---------- // + +void get_primitive(void) { + List list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.get(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_INT(1, list.get(0)); + TEST_ASSERT_EQUAL_INT(2, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void get_class(void) { + List list; + + list.add("1"); + + TEST_ASSERT_EQUAL_STRING("1", list.get(0).c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_STRING("1", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("2", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- T *getMutableValue(int index) ---------- // + +void getMutableValue_primitive(void) { + List list; + + list.add(1); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void getMutableValue_class(void) { + List list; + + list.add("1"); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- T operator[](int index) ---------- // + +void get_operator_primitive(void) { + List list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void get_operator_class(void) { + List list; + + list.add("1"); + + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- T get(int index) ---------- // + RUN_TEST(get_primitive); + RUN_TEST(get_class); + + // ---------- T *getMutableValue(int index) ---------- // + RUN_TEST(getMutableValue_primitive); + RUN_TEST(getMutableValue_class); + + // ---------- T operator[](int index) ---------- // + RUN_TEST(get_operator_primitive); + RUN_TEST(get_operator_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_list_remove/test_immutable_list_remove.cpp b/test/test_immutable_list_remove/test_immutable_list_remove.cpp new file mode 100644 index 0000000..35efcca --- /dev/null +++ b/test/test_immutable_list_remove/test_immutable_list_remove.cpp @@ -0,0 +1,244 @@ +#include + +#include "unity.h" + +#include + +// ---------- void clear() ---------- // + +void clear_primitive(void) { + List list; + + list.add(1); + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.clear(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +void clear_class(void) { + List list; + + list.add("1"); + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.clear(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +// ---------- void removeAll() ---------- // + +void removeAll_primitive(void) { + List list; + + list.add(1); + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeAll(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +void removeAll_class(void) { + List list; + + list.add("1"); + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeAll(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +// ---------- void remove(int index) ---------- // + +void remove_primitive(void) { + List list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.remove(0); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.remove(1); + + TEST_ASSERT_EQUAL_INT(2, list.get(0)); + TEST_ASSERT_EQUAL_INT(4, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void remove_class(void) { + List list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.remove(0); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.remove(1); + + TEST_ASSERT_EQUAL_STRING("2", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("4", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void removeFirst() ---------- // + +void removeFirst_primitive(void) { + List list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(3, list.get(0)); + TEST_ASSERT_EQUAL_INT(4, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void removeFirst_class(void) { + List list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_STRING("3", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("4", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void removeLast() ---------- // + +void removeLast_primitive(void) { + List list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(2, list.get(0)); + TEST_ASSERT_EQUAL_INT(3, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void removeLast_class(void) { + List list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_STRING("2", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("3", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- void clear() ---------- // + RUN_TEST(clear_primitive); + RUN_TEST(clear_class); + + // ---------- void removeAll() ---------- // + RUN_TEST(removeAll_primitive); + RUN_TEST(removeAll_class); + + // ---------- void remove(int index) ---------- // + RUN_TEST(remove_primitive); + RUN_TEST(remove_class); + + // ---------- void removeFirst() ---------- // + RUN_TEST(removeFirst_primitive); + RUN_TEST(removeFirst_class); + + // ---------- void removeLast() ---------- // + RUN_TEST(removeLast_primitive); + RUN_TEST(removeLast_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_+/test_immutable_singlelinkedlist_+.cpp b/test/test_immutable_singlelinkedlist_+/test_immutable_singlelinkedlist_+.cpp new file mode 100644 index 0000000..6bc230d --- /dev/null +++ b/test/test_immutable_singlelinkedlist_+/test_immutable_singlelinkedlist_+.cpp @@ -0,0 +1,246 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void add_ds_rvalue_primitive(void) { + SingleLinkedList list; + + { + list + 1; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list + 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_primitive(void) { + SingleLinkedList list; + + { + int a = 1; + list + a; + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list + b; + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_rvalue_class(void) { + SingleLinkedList list; + + { + list + "1"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list + "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_class(void) { + SingleLinkedList list; + + { + String a = "1"; + list + a; + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list + b; + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void add_ss_rvalue_primitive(void) { + SingleLinkedList list; + + list + 1; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_primitive(void) { + SingleLinkedList list; + + int a = 1, b = 2; + + list + a; + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + b; + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_rvalue_class(void) { + SingleLinkedList list; + + list + "1"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_class(void) { + SingleLinkedList list; + + String a = "1", b = "2"; + + list + a; + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list + b; + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void operator+(AbstractList &list) ---------- // + +void addAll_primitive(void) { + SingleLinkedList fromList; + SingleLinkedList toList; + + toList.add(1); + toList.add(2); + toList.add(3); + fromList.add(4); + fromList.add(5); + fromList.add(6); + + toList + fromList; + + TEST_ASSERT_EQUAL_INT(4, fromList[0]); + TEST_ASSERT_EQUAL_INT(5, fromList[1]); + TEST_ASSERT_EQUAL_INT(6, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_class(void) { + SingleLinkedList fromList; + SingleLinkedList toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + fromList.add("4"); + fromList.add("5"); + fromList.add("6"); + + toList + fromList; + + TEST_ASSERT_EQUAL_STRING("4", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(add_ds_rvalue_primitive); + RUN_TEST(add_ds_lvalue_primitive); + RUN_TEST(add_ds_rvalue_class); + RUN_TEST(add_ds_lvalue_class); + + // same scope tests + RUN_TEST(add_ss_rvalue_primitive); + RUN_TEST(add_ss_lvalue_primitive); + RUN_TEST(add_ss_rvalue_class); + RUN_TEST(add_ss_lvalue_class); + + // ---------- void operator+(AbstractList &list) ---------- // + RUN_TEST(addAll_primitive); + RUN_TEST(addAll_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_add/test_immutable_singlelinkedlist_add.cpp b/test/test_immutable_singlelinkedlist_add/test_immutable_singlelinkedlist_add.cpp new file mode 100644 index 0000000..7376a27 --- /dev/null +++ b/test/test_immutable_singlelinkedlist_add/test_immutable_singlelinkedlist_add.cpp @@ -0,0 +1,184 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void add_ds_rvalue_primitive(void) { + SingleLinkedList list; + + { + list.add(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.add(2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_primitive(void) { + SingleLinkedList list; + + { + int a = 1; + list.add(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.add(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_rvalue_class(void) { + SingleLinkedList list; + + { + list.add("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.add("2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ds_lvalue_class(void) { + SingleLinkedList list; + + { + String a = "1"; + list.add(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.add(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void add_ss_rvalue_primitive(void) { + SingleLinkedList list; + + list.add(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_primitive(void) { + SingleLinkedList list; + + int a = 1, b = 2; + + list.add(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_rvalue_class(void) { + SingleLinkedList list; + + list.add("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void add_ss_lvalue_class(void) { + SingleLinkedList list; + + String a = "1", b = "2"; + + list.add(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(add_ds_rvalue_primitive); + RUN_TEST(add_ds_lvalue_primitive); + RUN_TEST(add_ds_rvalue_class); + RUN_TEST(add_ds_lvalue_class); + + // same scope tests + RUN_TEST(add_ss_rvalue_primitive); + RUN_TEST(add_ss_lvalue_primitive); + RUN_TEST(add_ss_rvalue_class); + RUN_TEST(add_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_addAll/test_immutable_singlelinkedlist_addAll.cpp b/test/test_immutable_singlelinkedlist_addAll/test_immutable_singlelinkedlist_addAll.cpp new file mode 100644 index 0000000..381c04e --- /dev/null +++ b/test/test_immutable_singlelinkedlist_addAll/test_immutable_singlelinkedlist_addAll.cpp @@ -0,0 +1,262 @@ +#include + +#include "unity.h" + +#include + +// ---------- void addAll(AbstractList &list) ---------- // + +void addAll_list_primitive(void) { + SingleLinkedList fromList; + SingleLinkedList toList; + + toList.add(1); + toList.add(2); + toList.add(3); + fromList.add(4); + fromList.add(5); + fromList.add(6); + + toList.addAll(fromList); + + TEST_ASSERT_EQUAL_INT(4, fromList[0]); + TEST_ASSERT_EQUAL_INT(5, fromList[1]); + TEST_ASSERT_EQUAL_INT(6, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_list_class(void) { + SingleLinkedList fromList; + SingleLinkedList toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + fromList.add("4"); + fromList.add("5"); + fromList.add("6"); + + toList.addAll(fromList); + + TEST_ASSERT_EQUAL_STRING("4", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(int index, AbstractList &list) ---------- // + +void addAll_index_primitive(void) { + SingleLinkedList fromList; + SingleLinkedList toList; + + toList.add(1); + toList.add(5); + toList.add(6); + fromList.add(2); + fromList.add(3); + fromList.add(4); + + toList.addAll(1, fromList); + + TEST_ASSERT_EQUAL_INT(2, fromList[0]); + TEST_ASSERT_EQUAL_INT(3, fromList[1]); + TEST_ASSERT_EQUAL_INT(4, fromList[2]); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_index_class(void) { + SingleLinkedList fromList; + SingleLinkedList toList; + + toList.add("1"); + toList.add("5"); + toList.add("6"); + fromList.add("2"); + fromList.add("3"); + fromList.add("4"); + + toList.addAll(1, fromList); + + TEST_ASSERT_EQUAL_STRING("2", fromList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", fromList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("4", fromList[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, fromList.getSize()); + + fromList.clear(); + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(T *arr, size_t arrSize) ---------- // + +void addAll_array_primitive(void) { + int fromArray[] = {4, 5, 6}; + SingleLinkedList toList; + + toList.add(1); + toList.add(2); + toList.add(3); + + toList.addAll(fromArray, 3); + + TEST_ASSERT_EQUAL_INT(4, fromArray[0]); + TEST_ASSERT_EQUAL_INT(5, fromArray[1]); + TEST_ASSERT_EQUAL_INT(6, fromArray[2]); + + fromArray[0] = 7; + fromArray[1] = 8; + fromArray[2] = 9; + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_array_class(void) { + String fromArray[] = {"4", "5", "6"}; + SingleLinkedList toList; + + toList.add("1"); + toList.add("2"); + toList.add("3"); + + toList.addAll(fromArray, 3); + + TEST_ASSERT_EQUAL_STRING("4", fromArray[0].c_str()); + TEST_ASSERT_EQUAL_STRING("5", fromArray[1].c_str()); + TEST_ASSERT_EQUAL_STRING("6", fromArray[2].c_str()); + + fromArray[0] = "7"; + fromArray[1] = "8"; + fromArray[2] = "9"; + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +// ---------- void addAll(int index, T* arr, size_t arrSize) ---------- // + +void addAll_array_index_primitive(void) { + int fromArray[] = {2, 3, 4}; + SingleLinkedList toList; + + toList.add(1); + toList.add(5); + toList.add(6); + + toList.addAll(1, fromArray, 3); + + TEST_ASSERT_EQUAL_INT(2, fromArray[0]); + TEST_ASSERT_EQUAL_INT(3, fromArray[1]); + TEST_ASSERT_EQUAL_INT(4, fromArray[2]); + + fromArray[0] = 7; + fromArray[1] = 8; + fromArray[2] = 9; + + TEST_ASSERT_EQUAL_INT(1, toList[0]); + TEST_ASSERT_EQUAL_INT(2, toList[1]); + TEST_ASSERT_EQUAL_INT(3, toList[2]); + TEST_ASSERT_EQUAL_INT(4, toList[3]); + TEST_ASSERT_EQUAL_INT(5, toList[4]); + TEST_ASSERT_EQUAL_INT(6, toList[5]); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void addAll_array_index_class(void) { + String fromArray[] = {"2", "3", "4"}; + SingleLinkedList toList; + + toList.add("1"); + toList.add("5"); + toList.add("6"); + + toList.addAll(1, fromArray, 3); + + TEST_ASSERT_EQUAL_STRING("2", fromArray[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", fromArray[1].c_str()); + TEST_ASSERT_EQUAL_STRING("4", fromArray[2].c_str()); + + fromArray[0] = "7"; + fromArray[1] = "8"; + fromArray[2] = "9"; + + TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str()); + TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str()); + TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str()); + TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str()); + TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str()); + TEST_ASSERT_EQUAL_INT(6, toList.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- void addAll(AbstractList &list) ---------- // + RUN_TEST(addAll_list_primitive); + RUN_TEST(addAll_list_class); + + // ---------- void addAll(int index, AbstractList &list) ---------- // + RUN_TEST(addAll_index_primitive); + RUN_TEST(addAll_index_class); + + // ---------- void addAll(T *arr, size_t arrSize) ---------- // + RUN_TEST(addAll_array_primitive); + RUN_TEST(addAll_array_class); + + // ---------- void addAll(int index, T* arr, size_t arrSize) ---------- // + RUN_TEST(addAll_array_index_primitive); + RUN_TEST(addAll_array_index_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_addAtIndex/test_immutable_singlelinkedlist_addAtIndex.cpp b/test/test_immutable_singlelinkedlist_addAtIndex/test_immutable_singlelinkedlist_addAtIndex.cpp new file mode 100644 index 0000000..cb574d0 --- /dev/null +++ b/test/test_immutable_singlelinkedlist_addAtIndex/test_immutable_singlelinkedlist_addAtIndex.cpp @@ -0,0 +1,252 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addAtIndex_ds_rvalue_primitive(void) { + SingleLinkedList list; + + { + list.addAtIndex(0, 1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addAtIndex(1, 2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + list.addAtIndex(1, 3); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_lvalue_primitive(void) { + SingleLinkedList list; + + { + int a = 1; + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + int c = 3; + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_INT(3, c); + c = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_rvalue_class(void) { + SingleLinkedList list; + + { + list.addAtIndex(0, "1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addAtIndex(1, "2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + list.addAtIndex(1, "3"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ds_lvalue_class(void) { + SingleLinkedList list; + + { + String a = "1"; + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + { + String c = "3"; + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_STRING("3", c.c_str()); + c = "4"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addAtIndex_ss_rvalue_primitive(void) { + SingleLinkedList list; + + list.addAtIndex(0, 1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, 2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, 3); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_lvalue_primitive(void) { + SingleLinkedList list; + + int a = 1, b = 2, c = 3; + + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_INT(3, c); + c = 4; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(3, list[1]); + TEST_ASSERT_EQUAL_INT(2, list[2]); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_rvalue_class(void) { + SingleLinkedList list; + + list.addAtIndex(0, "1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, "2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, "3"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void addAtIndex_ss_lvalue_class(void) { + SingleLinkedList list; + + String a = "1", b = "2", c = "3"; + + list.addAtIndex(0, a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addAtIndex(1, b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); + + list.addAtIndex(1, c); + + TEST_ASSERT_EQUAL_STRING("3", c.c_str()); + c = "4"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("3", list[1].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[2].c_str()); + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addAtIndex_ds_rvalue_primitive); + RUN_TEST(addAtIndex_ds_lvalue_primitive); + RUN_TEST(addAtIndex_ds_rvalue_class); + RUN_TEST(addAtIndex_ds_lvalue_class); + + // same scope tests + RUN_TEST(addAtIndex_ss_rvalue_primitive); + RUN_TEST(addAtIndex_ss_lvalue_primitive); + RUN_TEST(addAtIndex_ss_rvalue_class); + RUN_TEST(addAtIndex_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_addFirst/test_immutable_singlelinkedlist_addFirst.cpp b/test/test_immutable_singlelinkedlist_addFirst/test_immutable_singlelinkedlist_addFirst.cpp new file mode 100644 index 0000000..8fff651 --- /dev/null +++ b/test/test_immutable_singlelinkedlist_addFirst/test_immutable_singlelinkedlist_addFirst.cpp @@ -0,0 +1,182 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addFirst_ds_rvalue_primitive(void) { + SingleLinkedList list; + + { + list.addFirst(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addFirst(2); + } + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_lvalue_primitive(void) { + SingleLinkedList list; + + { + int a = 1; + list.addFirst(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addFirst(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_rvalue_class(void) { + SingleLinkedList list; + + { + list.addFirst("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addFirst("2"); + } + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ds_lvalue_class(void) { + SingleLinkedList list; + + { + String a = "1"; + list.addFirst(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addFirst(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addFirst_ss_rvalue_primitive(void) { + SingleLinkedList list; + + list.addFirst(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(2); + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_lvalue_primitive(void) { + SingleLinkedList list; + + int a = 1, b = 2; + + list.addFirst(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(2, list[0]); + TEST_ASSERT_EQUAL_INT(1, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_rvalue_class(void) { + SingleLinkedList list; + + list.addFirst("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst("2"); + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addFirst_ss_lvalue_class(void) { + SingleLinkedList list; + + String a = "1", b = "2"; + + list.addFirst(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addFirst(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("2", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("1", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addFirst_ds_rvalue_primitive); + RUN_TEST(addFirst_ds_lvalue_primitive); + RUN_TEST(addFirst_ds_rvalue_class); + RUN_TEST(addFirst_ds_lvalue_class); + + // same scope tests + RUN_TEST(addFirst_ss_rvalue_primitive); + RUN_TEST(addFirst_ss_lvalue_primitive); + RUN_TEST(addFirst_ss_rvalue_class); + RUN_TEST(addFirst_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_addLast/test:immutable_singlelinkedlist_addLast.cpp b/test/test_immutable_singlelinkedlist_addLast/test:immutable_singlelinkedlist_addLast.cpp new file mode 100644 index 0000000..3259f18 --- /dev/null +++ b/test/test_immutable_singlelinkedlist_addLast/test:immutable_singlelinkedlist_addLast.cpp @@ -0,0 +1,182 @@ +#include + +#include "unity.h" + +#include + +// ---------- In different scope (ds) ---------- // + +void addLast_ds_rvalue_primitive(void) { + SingleLinkedList list; + + { + list.addLast(1); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addLast(2); + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_lvalue_primitive(void) { + SingleLinkedList list; + + { + int a = 1; + list.addLast(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + int b = 2; + list.addLast(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + } + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_rvalue_class(void) { + SingleLinkedList list; + + { + list.addLast("1"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + list.addLast("2"); + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ds_lvalue_class(void) { + SingleLinkedList list; + + { + String a = "1"; + list.addLast(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + { + String b = "2"; + list.addLast(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + } + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- In same scope (ss) ---------- // + +void addLast_ss_rvalue_primitive(void) { + SingleLinkedList list; + + list.addLast(1); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(2); + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_lvalue_primitive(void) { + SingleLinkedList list; + + int a = 1, b = 2; + + list.addLast(a); + + TEST_ASSERT_EQUAL_INT(1, a); + a = 2; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(b); + + TEST_ASSERT_EQUAL_INT(2, b); + b = 3; + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_rvalue_class(void) { + SingleLinkedList list; + + list.addLast("1"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast("2"); + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void addLast_ss_lvalue_class(void) { + SingleLinkedList list; + + String a = "1", b = "2"; + + list.addLast(a); + + TEST_ASSERT_EQUAL_STRING("1", a.c_str()); + a = "2"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.addLast(b); + + TEST_ASSERT_EQUAL_STRING("2", b.c_str()); + b = "3"; + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // different scope tests + RUN_TEST(addLast_ds_rvalue_primitive); + RUN_TEST(addLast_ds_lvalue_primitive); + RUN_TEST(addLast_ds_rvalue_class); + RUN_TEST(addLast_ds_lvalue_class); + + // same scope tests + RUN_TEST(addLast_ss_rvalue_primitive); + RUN_TEST(addLast_ss_lvalue_primitive); + RUN_TEST(addLast_ss_rvalue_class); + RUN_TEST(addLast_ss_lvalue_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_diverse/test_immutable_singlelinkedlist_diverse.cpp b/test/test_immutable_singlelinkedlist_diverse/test_immutable_singlelinkedlist_diverse.cpp new file mode 100644 index 0000000..f52940c --- /dev/null +++ b/test/test_immutable_singlelinkedlist_diverse/test_immutable_singlelinkedlist_diverse.cpp @@ -0,0 +1,112 @@ +#include + +#include "unity.h" + +#include + +// ---------- getSize ---------- // + +void getSize_primitive(void) { + SingleLinkedList list; + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +void getSize_class(void) { + SingleLinkedList list; + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); +} + +// ---------- is mutable ---------- // + +void isMutable_primitive(void) { + SingleLinkedList list; + + TEST_ASSERT_FALSE(list.isMutable()); +} + +void isMutable_class(void) { + SingleLinkedList list; + + TEST_ASSERT_FALSE(list.isMutable()); +} + +// ---------- is empty ---------- // + +void isEmpty_primitive(void) { + SingleLinkedList list; + + TEST_ASSERT_TRUE(list.isEmpty()); + + list.add(1); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.add(2); + list.add(3); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.clear(); + + TEST_ASSERT_TRUE(list.isEmpty()); +} + +void isEmpty_class(void) { + SingleLinkedList list; + + TEST_ASSERT_TRUE(list.isEmpty()); + + list.add("1"); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.add("2"); + list.add("3"); + + TEST_ASSERT_FALSE(list.isEmpty()); + + list.clear(); + + TEST_ASSERT_TRUE(list.isEmpty()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- getSize ---------- // + RUN_TEST(getSize_primitive); + RUN_TEST(getSize_class); + + // ---------- is mutable ---------- // + RUN_TEST(isMutable_primitive); + RUN_TEST(isMutable_class); + + // ---------- is empty ---------- // + RUN_TEST(isEmpty_primitive); + RUN_TEST(isEmpty_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_equality/test_immutable_singlelinkedlist_equality.cpp b/test/test_immutable_singlelinkedlist_equality/test_immutable_singlelinkedlist_equality.cpp new file mode 100644 index 0000000..b18b6a0 --- /dev/null +++ b/test/test_immutable_singlelinkedlist_equality/test_immutable_singlelinkedlist_equality.cpp @@ -0,0 +1,216 @@ +#include + +#include "unity.h" + +#include + +// ---------- bool equals(AbstractList &other) ---------- // + +void equals_primitive(void) { + SingleLinkedList list; + SingleLinkedList other; + + TEST_ASSERT_TRUE(list.equals(other)); + + list.add(1); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_TRUE(list.equals(other)); + + other.add(3); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add(3); + + TEST_ASSERT_TRUE(list.equals(other)); + + list.clear(); + + SingleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list.equals(mutableList)); +} + +void equals_class(void) { + SingleLinkedList list; + SingleLinkedList other; + + TEST_ASSERT_TRUE(list.equals(other)); + + list.add("1"); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_TRUE(list.equals(other)); + + other.add("3"); + + TEST_ASSERT_FALSE(list.equals(other)); + + list.add("3"); + + TEST_ASSERT_TRUE(list.equals(other)); + + list.clear(); + + SingleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list.equals(mutableList)); +} + +// ---------- bool operator==(AbstractList &other) ---------- // +void equals_operator_primitive(void) { + SingleLinkedList list; + SingleLinkedList other; + + TEST_ASSERT_TRUE(list == other); + + list.add(1); + + TEST_ASSERT_FALSE(list == other); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_TRUE(list == other); + + other.add(3); + + TEST_ASSERT_FALSE(list == other); + + list.add(3); + + TEST_ASSERT_TRUE(list == other); + + list.clear(); + + SingleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list == mutableList); +} + +void equals_operator_class(void) { + SingleLinkedList list; + SingleLinkedList other; + + TEST_ASSERT_TRUE(list == other); + + list.add("1"); + + TEST_ASSERT_FALSE(list == other); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_TRUE(list == other); + + other.add("3"); + + TEST_ASSERT_FALSE(list == other); + + list.add("3"); + + TEST_ASSERT_TRUE(list == other); + + list.clear(); + + SingleLinkedList mutableList(true); + + TEST_ASSERT_FALSE(list == mutableList); +} + +// ---------- bool operator!=(AbstractList &other) ---------- // +void not_equals_operator_primitive(void) { + SingleLinkedList list; + SingleLinkedList other; + + TEST_ASSERT_FALSE(list != other); + + list.add(1); + + TEST_ASSERT_TRUE(list != other); + + list.add(2); + other.add(1); + other.add(2); + + TEST_ASSERT_FALSE(list != other); + + other.add(3); + + TEST_ASSERT_TRUE(list != other); + + list.add(3); + + TEST_ASSERT_FALSE(list != other); + + list.clear(); + + SingleLinkedList mutableList(true); + + TEST_ASSERT_TRUE(list != mutableList); +} + +void not_equals_operator_class(void) { + SingleLinkedList list; + SingleLinkedList other; + + TEST_ASSERT_FALSE(list != other); + + list.add("1"); + + TEST_ASSERT_TRUE(list != other); + + list.add("2"); + other.add("1"); + other.add("2"); + + TEST_ASSERT_FALSE(list != other); + + other.add("3"); + + TEST_ASSERT_TRUE(list != other); + + list.add("3"); + + TEST_ASSERT_FALSE(list != other); + + list.clear(); + + SingleLinkedList mutableList(true); + + TEST_ASSERT_TRUE(list != mutableList); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- bool equals(AbstractList &other) ---------- // + RUN_TEST(equals_primitive); + RUN_TEST(equals_class); + + // ---------- bool operator==(AbstractList &other) ---------- // + RUN_TEST(equals_operator_primitive); + RUN_TEST(equals_operator_class); + + // ---------- bool operator!=(AbstractList &other) ---------- // + RUN_TEST(not_equals_operator_primitive); + RUN_TEST(not_equals_operator_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_get/test_immutable_singlelinkedlist_get.cpp b/test/test_immutable_singlelinkedlist_get/test_immutable_singlelinkedlist_get.cpp new file mode 100644 index 0000000..62fd60d --- /dev/null +++ b/test/test_immutable_singlelinkedlist_get/test_immutable_singlelinkedlist_get.cpp @@ -0,0 +1,122 @@ +#include + +#include "unity.h" + +#include + +// ---------- T get(int index) ---------- // + +void get_primitive(void) { + SingleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.get(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_INT(1, list.get(0)); + TEST_ASSERT_EQUAL_INT(2, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void get_class(void) { + SingleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_STRING("1", list.get(0).c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_STRING("1", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("2", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- T *getMutableValue(int index) ---------- // + +void getMutableValue_primitive(void) { + SingleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void getMutableValue_class(void) { + SingleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(0)); + TEST_ASSERT_EQUAL_PTR(nullptr, list.getMutableValue(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- T operator[](int index) ---------- // + +void get_operator_primitive(void) { + SingleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add(2); + + TEST_ASSERT_EQUAL_INT(1, list[0]); + TEST_ASSERT_EQUAL_INT(2, list[1]); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void get_operator_class(void) { + SingleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.add("2"); + + TEST_ASSERT_EQUAL_STRING("1", list[0].c_str()); + TEST_ASSERT_EQUAL_STRING("2", list[1].c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- T get(int index) ---------- // + RUN_TEST(get_primitive); + RUN_TEST(get_class); + + // ---------- T *getMutableValue(int index) ---------- // + RUN_TEST(getMutableValue_primitive); + RUN_TEST(getMutableValue_class); + + // ---------- T operator[](int index) ---------- // + RUN_TEST(get_operator_primitive); + RUN_TEST(get_operator_class); + + UNITY_END(); +} + +void loop() { +} diff --git a/test/test_immutable_singlelinkedlist_remove/test_immutable_singlelinkedlist_remove.cpp b/test/test_immutable_singlelinkedlist_remove/test_immutable_singlelinkedlist_remove.cpp new file mode 100644 index 0000000..db0aeb1 --- /dev/null +++ b/test/test_immutable_singlelinkedlist_remove/test_immutable_singlelinkedlist_remove.cpp @@ -0,0 +1,244 @@ +#include + +#include "unity.h" + +#include + +// ---------- void clear() ---------- // + +void clear_primitive(void) { + SingleLinkedList list; + + list.add(1); + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.clear(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +void clear_class(void) { + SingleLinkedList list; + + list.add("1"); + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.clear(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +// ---------- void removeAll() ---------- // + +void removeAll_primitive(void) { + SingleLinkedList list; + + list.add(1); + list.add(2); + list.add(3); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeAll(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +void removeAll_class(void) { + SingleLinkedList list; + + list.add("1"); + list.add("2"); + list.add("3"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeAll(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); +} + +// ---------- void remove(int index) ---------- // + +void remove_primitive(void) { + SingleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.remove(0); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.remove(1); + + TEST_ASSERT_EQUAL_INT(2, list.get(0)); + TEST_ASSERT_EQUAL_INT(4, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void remove_class(void) { + SingleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.remove(0); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.remove(1); + + TEST_ASSERT_EQUAL_STRING("2", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("4", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void removeFirst() ---------- // + +void removeFirst_primitive(void) { + SingleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(3, list.get(0)); + TEST_ASSERT_EQUAL_INT(4, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void removeFirst_class(void) { + SingleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeFirst(); + + TEST_ASSERT_EQUAL_STRING("3", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("4", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +// ---------- void removeLast() ---------- // + +void removeLast_primitive(void) { + SingleLinkedList list; + + list.add(1); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add(2); + list.add(3); + list.add(4); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(2, list.get(0)); + TEST_ASSERT_EQUAL_INT(3, list.get(1)); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void removeLast_class(void) { + SingleLinkedList list; + + list.add("1"); + + TEST_ASSERT_EQUAL_INT(1, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_INT(0, list.getSize()); + + list.add("2"); + list.add("3"); + list.add("4"); + + TEST_ASSERT_EQUAL_INT(3, list.getSize()); + + list.removeLast(); + + TEST_ASSERT_EQUAL_STRING("2", list.get(0).c_str()); + TEST_ASSERT_EQUAL_STRING("3", list.get(1).c_str()); + TEST_ASSERT_EQUAL_INT(2, list.getSize()); +} + +void setup() { + UNITY_BEGIN(); + + // ---------- void clear() ---------- // + RUN_TEST(clear_primitive); + RUN_TEST(clear_class); + + // ---------- void removeAll() ---------- // + RUN_TEST(removeAll_primitive); + RUN_TEST(removeAll_class); + + // ---------- void remove(int index) ---------- // + RUN_TEST(remove_primitive); + RUN_TEST(remove_class); + + // ---------- void removeFirst() ---------- // + RUN_TEST(removeFirst_primitive); + RUN_TEST(removeFirst_class); + + // ---------- void removeLast() ---------- // + RUN_TEST(removeLast_primitive); + RUN_TEST(removeLast_class); + + UNITY_END(); +} + +void loop() { +}