From 315a200c9b359bbfc767ffea89a281d25fa65b10 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Sun, 24 Nov 2024 22:25:54 -0500 Subject: [PATCH] committing more tests --- minimal.cpp | 122 ++++++++++ morseex.cpp | 67 +++++ ta.cpp | 35 +++ test-kec.cpp | 620 +++++++++++++++++++++++++++++++++++++++++++++++ test-scaling.cpp | 620 +++++++++++++++++++++++++++++++++++++++++++++++ test.cpp | 140 +++++++++++ 6 files changed, 1604 insertions(+) create mode 100644 minimal.cpp create mode 100644 morseex.cpp create mode 100644 ta.cpp create mode 100644 test-kec.cpp create mode 100644 test-scaling.cpp create mode 100644 test.cpp diff --git a/minimal.cpp b/minimal.cpp new file mode 100644 index 0000000..ef7ab88 --- /dev/null +++ b/minimal.cpp @@ -0,0 +1,122 @@ +#include "Map.hpp" + +#include + +// basically an int wrapper +class MyKeyType { + private: + int val; + + public: + //not default constructable, not copy assignable, not move assignable + MyKeyType() = delete; + MyKeyType& operator=(const MyKeyType&) = delete; + MyKeyType& operator=(MyKeyType&&) = delete; + + // copy constructable and move assignable + MyKeyType(MyKeyType&&) = default; + MyKeyType(const MyKeyType&) = default; + ~MyKeyType() = default; + + MyKeyType(int i) : val(i) { } + + bool operator<(const MyKeyType& other) const { + return this->val < other.val; + } + + bool operator==(const MyKeyType &other) const { + return this->val == other.val; + } +}; + +// same as keytype except no operator< +class MyValueType { + private: + int val; + + public: + //not default constructable, not copy assignable, not move assignable + MyValueType() = delete; + MyValueType& operator=(const MyValueType&) = delete; + MyValueType& operator=(MyValueType&&) = delete; + + // copy constructable and move assignable + MyValueType(MyValueType&&) = default; + MyValueType(const MyValueType&) = default; + ~MyValueType() = default; + + MyValueType(int i) : val(i) { } + + bool operator==(const MyValueType &other) const { + return this->val == other.val; + } +}; + +class MyDefaultConstructible { + + friend bool operator<(const MyDefaultConstructible &o1, const MyDefaultConstructible &o2) { + return o1.val < o2.val; + } + + private: + int val = 0; + + public: + // not copy assignable, not move assignable + MyDefaultConstructible& operator=(const MyDefaultConstructible&) = delete; + MyDefaultConstructible& operator=(MyDefaultConstructible&&) = delete; + + // default constructable, copy constructable and move assignable + MyDefaultConstructible() = default; + MyDefaultConstructible(MyDefaultConstructible&&) = default; + MyDefaultConstructible(const MyDefaultConstructible&) = default; + ~MyDefaultConstructible() = default; + + MyDefaultConstructible(int i) : val(i) { } + + bool operator==(const MyDefaultConstructible &other) const { + return this->val == other.val; + } +}; + + +class MyAssignable { + private: + int val = 0; + + public: + MyAssignable() = default; + MyAssignable(int i) : val(i) { } + bool operator==(const MyAssignable &other) const { + return this->val == other.val; + } +}; + +// manual instantiation, instantiates every member function instead of +// just the ones called +template class cs440::Map; + + +int main() { + cs440::Map m{{3, 5}}; + m.insert({{2}, {3}}); + m.insert({{1}, {3}}); + m.insert({{5}, {3}}); + m.insert({{7}, {3}}); + m.at(2); + auto iter = m.find(2); + m.erase(iter); + auto m_copy = m; + assert(m_copy == m); + + cs440::Map m2{{8, 9}}; + m2[10]; // should default construct these values + m2[15]; + + cs440::Map m3{{6, 7}}; + m3[20] = {5}; // move assign + MyAssignable ma{1}; + m3[10] = ma; //copy assign + + return 0; +} diff --git a/morseex.cpp b/morseex.cpp new file mode 100644 index 0000000..a538017 --- /dev/null +++ b/morseex.cpp @@ -0,0 +1,67 @@ +#include "Map.hpp" + +#include +#include +#include +#include + + +cs440::Map morse { + {',', "--..--"}, + {'.', ".-.-.-"}, + {'?', "..--.."}, + {'0', "----- "}, + {'1', ".---- "}, + {'2', "..--- "}, + {'3', "...-- "}, + {'4', "....- "}, + {'5', "..... "}, + {'6', "-.... "}, + {'7', "--... "}, + {'8', "---.. "}, + {'9', "----. "}, + {'A', ".-"}, + {'B', "-..."}, + {'C', "-.-."}, + {'D', "-.. "}, + {'E', "."}, + {'F', "..-."}, + {'G', "--."}, + {'H', "...."}, + {'I', ".."}, + {'J', ".---"}, + {'K', "-.-"}, + {'L', ".-.."}, + {'M', "--"}, + {'N', "-."}, + {'O', "---"}, + {'P', ".--."}, + {'Q', "--.-"}, + {'R', ".-."}, + {'S', "..."}, + {'T', "-"}, + {'U', "..-"}, + {'V', "...-"}, + {'W', ".--"}, + {'X', "-..-"}, + {'Y', "-.--"}, + {'Z', "--.."}, +}; + + +int main() { + std::cout << "Enter messages to be translated (CTRL+D to exit)\n"; + + std::string message; + while(std::cin >> message) { + for (auto c : message) { + try { + std::cout << morse.at(toupper(c)) << '\n'; + } catch (std::out_of_range) { + std::cout << "invalid character: " << c << '\n'; + } + } + } + + return 0; +} diff --git a/ta.cpp b/ta.cpp new file mode 100644 index 0000000..8de3646 --- /dev/null +++ b/ta.cpp @@ -0,0 +1,35 @@ +#include "Map.hpp" +#include +#include + +template class cs440::Map; +int main(void) { + cs440::Map a; + std::stringstream ss; + std::string s; + for (std::size_t i = 10; i >= 1; i--) { + ss << i; + ss >> s; + a.insert({s, i}); + for (std::size_t j = 10; j >= i; j--) { + ss << j; + ss >> s; + } + } + for (std::size_t i = 1; i <= 10; i++) { + ss << i; + ss >> s; + } + for (std::size_t i = 10; i >= 5; i--) { + std::cout << i << std::endl; + ss << i; + ss >> s; + auto b = a.find(s); + a.erase(b); + for (std::size_t j = 1; j <= i; j++) { + ss << j; + ss >> s; + } + } + return 0; +} diff --git a/test-kec.cpp b/test-kec.cpp new file mode 100644 index 0000000..4231aec --- /dev/null +++ b/test-kec.cpp @@ -0,0 +1,620 @@ +/* + * Run with + * + * -i iterations + * + * to do a stress test for the given number of iterations. + * + * -p + * + * to print correct output. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Map.hpp" + +/* + * Wrapper class around std::map to handle slight difference in return value and also + * provide an Iterator nested name. + */ + +template +class test_map : public std::map { + private: + using base_t = std::map; + public: + using Iterator = typename base_t::iterator; + std::pairinsert(const std::pair &p) { + return this->base_t::insert(p); + } +}; + +/* + * Person class. + */ + +struct Person { + friend bool operator<(const Person &p1, const Person &p2) { + return p1.name < p2.name; + } + friend bool operator==(const Person &p1, const Person &p2) { + return p1.name == p2.name; + } + Person(const char *n) : name(n) {} + void print() const { + printf("Name: %s\n", name.c_str()); + } + const std::string name; + Person &operator=(const Person &) = delete; +}; + +void +print(const std::pair &p) { + p.first.print(); + printf(" %d\n", p.second); +} + +/* + * MyClass class. + */ + +struct MyClass { + friend bool operator<(const MyClass &o1, const MyClass &o2) { + return o1.num < o2.num; + } + friend bool operator==(const MyClass &o1, const MyClass &o2) { + return o1.num == o2.num; + } + MyClass(double n) : num(n) {} + double num; +}; + +void +print(const std::pair &p) { + printf("%d, %s; ", p.first, p.second.c_str()); +} + +/* + * Stress class. + */ + +struct Stress { + friend bool operator<(const Stress& o1, const Stress& o2) { + return o1.val < o2.val; + } + friend bool operator==(const Stress& o1, const Stress& o2) { + return o1.val == o2.val; + } + Stress(int _v) : val(_v){} + int val; +}; +// Helper function for stress testing. This orders iterators by what they point to. +template