From 62b0c887f84ffee0b6c23874ec592deb1066f376 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 24 Jun 2020 16:54:28 +0000 Subject: [PATCH 1/8] added nostd::list and nostd::pair --- api/include/opentelemetry/nostd/list.h | 241 +++++++++++++++++++++++++ api/include/opentelemetry/nostd/pair.h | 51 ++++++ api/test/nostd/list_test.cc | 128 +++++++++++++ api/test/nostd/pair_test.cc | 17 ++ 4 files changed, 437 insertions(+) create mode 100644 api/include/opentelemetry/nostd/list.h create mode 100644 api/include/opentelemetry/nostd/pair.h create mode 100644 api/test/nostd/list_test.cc create mode 100644 api/test/nostd/pair_test.cc diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h new file mode 100644 index 0000000000..c19ee561be --- /dev/null +++ b/api/include/opentelemetry/nostd/list.h @@ -0,0 +1,241 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace nostd +{ + /* Non-standard implementation of the list data structure. */ + template + class list { + public: + + typedef T value_type; + + /* Custom iterator class*/ + class iterator + { + public: + typedef std::forward_iterator_tag iterator_category; + iterator(T* ptr) : ptr_(ptr) {} + iterator operator++() {ptr_++; return ptr_; } + T& operator*() { return *ptr_; } + T* operator->() { return ptr_; } + bool operator==(const iterator& other) {return ptr_ == other.ptr_; } + bool operator!=(const iterator& other) {return ptr_ != other.ptr_; } + + private: + T* ptr_; + }; + + list(){ + size_ = 0; + data_ = NULL; + }; + + /* Constructs class with passed in element. */ + list(T element){ + size_ = 1; + + T * data_ = new T [size_]; + + std::memcpy(data_, &element, sizeof(T)); + }; + + /* Constructs class with the passed in initializer_list. */ + list(std::initializer_list init_list){ + size_ = init_list.size(); + + T * temp = new T[size_]; + + std::memcpy(temp, std::begin(init_list), init_list.size()*sizeof(T)); + + data_ = temp; + } + + /* Returns the element at the front of the list. */ + T front(){ + if(size_ == 0){ + return T(); + } + return data_[0]; + } + + /* Returns the element at the back of the list. */ + T back(){ + if(size_ == 0){ + return T(); + } + return data_[size_ - 1]; + } + + /* Returns an iterator pointing at the front of the list. */ + iterator begin(){ + return iterator(data_); + } + + /* Returns an iterator pointing at the back of the list. */ + iterator end(){ + return iterator(data_ + size_); + } + + /* Inserts the passed in element at the passed in + * iterator position. + */ + iterator insert(iterator position, const T& element){ + int previous_size = size_; + size_++; + T *temp = new T[size_]; + + /* Counter tracks the index of the passed in iterator position. */ + int counter = 0; + for(auto iter = begin(); iter != end(); ++iter){ + if(iter == position){ + break; + } + counter++; + } + + int insert_index = counter; + int next_index = counter+1; + int last_index = previous_size; + int remaining_elements = previous_size - counter; + + /* Copy the original data that comes before the insertion. */ + std::memcpy(temp, data_, insert_index*sizeof(T)); + /* Copy the inserted data. */ + std::memcpy(temp + insert_index, &element, sizeof(T)); + /* Copy the original data that comes after the insertion. */ + std::memcpy(temp + next_index, data_+counter , remaining_elements*sizeof(T)); + + T *old_data = data_; + + data_ = temp; + + auto iter_pos = begin(); + /* Iterate until the inserted element. */ + for(int i = 0; i < insert_index; i++){ + ++iter_pos; + } + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + /* Return an iterator at the inserted element. */ + return iter_pos; + } + + /* Insert an element at the end of the list. */ + void push_back(T element){ + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + std::memcpy(temp, data_, previous_size*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + data_[previous_size] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Insert an element at the front of the list. */ + void push_front(T element){ + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + /* Copy the original data, leaving space for one element + * at the front. */ + std::memcpy(temp + 1, data_, previous_size*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + data_[0] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Returns the last element and removes it from the list. */ + T pop_back(){ + T back_elem = data_[size_ - 1]; + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_, size_*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return back_elem; + } + + /* Returns the first element and removes it from the list. */ + T pop_front(){ + T front_elem = front(); + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_ + 1, size_*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return front_elem; + } + + /* Returns the number of elements in the list. */ + int size(){ + return size_; + } + + void operator=(const list& other_list){ + data_ = other_list.data_; + size_ = other_list.size_; + } + + T& operator[](int index){ + return data_[index]; + } + + private: + T* data_; + size_t size_; + }; + +} +OPENTELEMETRY_END_NAMESPACE + + diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h new file mode 100644 index 0000000000..bf80805c30 --- /dev/null +++ b/api/include/opentelemetry/nostd/pair.h @@ -0,0 +1,51 @@ +#pragma once + +#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/version.h" + + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace nostd +{ + +/* The nostd::pair class provides a data structure capable of hold + * two values of different or the same types, and provides access to each. + */ +template +class pair { + public: + + pair(){ + first_ = T1(); + second_ = T2(); + } + + /* Constructs a pair object with the passed in first and second values. */ + pair( T1 first, T2 second){ + first_ = first; + second_ = second; + } + + void operator=( const pair& other_pair){ + first_ = other_pair.first_; + second_ = other_pair.second_; + } + + /* Returns the first element. */ + T1 first(){ + return first_; + } + + /* Returns the second element. */ + T2 second(){ + return second_; + } + + private: + T1 first_; + T2 second_; + +}; + +} +OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc new file mode 100644 index 0000000000..b75962e4b6 --- /dev/null +++ b/api/test/nostd/list_test.cc @@ -0,0 +1,128 @@ +#include "opentelemetry/nostd/list.h" +#include "opentelemetry/nostd/pair.h" + +#include + +using opentelemetry::nostd::list; +using opentelemetry::nostd::pair; + +/* Tests that front() returns the first element and back() + * returns the last element. + */ +TEST(ListTest, FrontBack) +{ + list list_1; + + list_1.push_back(9); + list_1.push_back(8); + list_1.push_back(7); + list_1.push_back(6); + + EXPECT_EQ(list_1.front(), 9); + EXPECT_EQ(list_1.back(), 6); + +} + +/* Tests that the list object can handle data types of a size + * other than an int. */ +TEST(ListTest, PairList) +{ + list> pair_list; + + pair_list.push_back(pair(1,2)); + pair_list.push_back(pair(2,3)); + pair_list.push_back(pair(4,5)); + + pair temp_pair; + temp_pair = pair_list.front(); + + EXPECT_EQ(pair_list.front().first(), 1); + EXPECT_EQ(pair_list.front().second(), 2); + + EXPECT_EQ(pair_list.back().first(), 4); + EXPECT_EQ(pair_list.back().second(), 5); +} + +/* Tests taht a list object can be initialized with an initalizer list. */ +TEST(ListTest, InitializerList) +{ + + list pair_list = {1,2,3,4,5,6,7,8,9,10}; + + EXPECT_EQ(pair_list.front(), 1); + EXPECT_EQ(pair_list.back(), 10); +} + +/* Tests that the list object can handle an insert_iterator. */ +TEST(ListTest, IteratorInsertion) +{ + + list int_list = {1,2,3,4}; + list insert_list = {5,6,7,8}; + + std::insert_iterator> back(int_list, std::end(int_list)); + + for (auto iter = std::begin(insert_list); iter != std::end(insert_list); ++iter) + { + back = *iter; + } + + int counter = 1; + for(auto iter = std::begin(int_list); iter != std::end(int_list); ++iter){ + EXPECT_EQ(*iter,counter); + counter++; + } +} + +/* Tests that the push_front method added elements to the front of the list. */ +TEST(ListTest, PushFront){ + list> list_list; + list inner_list = {0,1,2,3,4}; + list mid_list = {9,9,9,9,9}; + list_list.push_front(inner_list); + list_list.push_back(mid_list); + list_list.push_front(inner_list); + + EXPECT_EQ(list_list.front().front(),0); + EXPECT_EQ(list_list.front().back(),4); + + EXPECT_EQ(list_list.back().front(), 9); + EXPECT_EQ(list_list.back().back(), 9); +} + +/* Tests that you can modify elements via the subscript operator. */ +TEST(ListTest, SubscriptOperator){ + list int_list = {0,1,2,3,4}; + + EXPECT_EQ(int_list[2],2); + int_list[2] = 9; + EXPECT_EQ(int_list[2],9); +} + +/* Tests that the pop_back method, returns and removes the back element + * from the list. + */ +TEST(ListTest, PopBack){ + list int_list = {0,1,2,3,4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.back(),4); + EXPECT_EQ(int_list.pop_back(), 4); + EXPECT_EQ(int_list.size(), previous_size - 1); + + EXPECT_EQ(int_list.back(), 3); +} + +/* Tests that the pop_front method, returns and removes the front element + * from the list. + */ +TEST(ListTest, PopFront){ + list int_list = {0,1,2,3,4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.front(),0); + EXPECT_EQ(int_list.pop_front(), 0); + EXPECT_EQ(int_list.size(), previous_size - 1); + + EXPECT_EQ(int_list.front(), 1); +} diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc new file mode 100644 index 0000000000..ad2dc379f3 --- /dev/null +++ b/api/test/nostd/pair_test.cc @@ -0,0 +1,17 @@ +#include "opentelemetry/nostd/pair.h" + +#include + +using opentelemetry::nostd::pair; + +/* Tests constructing a pair object. */ +TEST(PairTest, BasicConstruction) +{ + int val_1 = 1; + int val_2 = 9; + pair temp_pair = pair(val_1,val_2); + + EXPECT_EQ(temp_pair.first(), val_1); + EXPECT_EQ(temp_pair.second(), val_2); + +} From 9c395365b021a0fd5a2ace2f5871fdc2924f3548 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 24 Jun 2020 16:58:59 +0000 Subject: [PATCH 2/8] ran auto formatter --- api/include/opentelemetry/nostd/list.h | 463 +++++++++++++------------ api/include/opentelemetry/nostd/pair.h | 72 ++-- api/test/nostd/list_test.cc | 88 ++--- api/test/nostd/pair_test.cc | 7 +- 4 files changed, 319 insertions(+), 311 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index c19ee561be..1d6e48f063 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -1,10 +1,10 @@ #pragma once +#include #include -#include #include -#include #include +#include #include "opentelemetry/nostd/utility.h" #include "opentelemetry/version.h" @@ -12,230 +12,239 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { - /* Non-standard implementation of the list data structure. */ - template - class list { - public: - - typedef T value_type; - - /* Custom iterator class*/ - class iterator - { - public: - typedef std::forward_iterator_tag iterator_category; - iterator(T* ptr) : ptr_(ptr) {} - iterator operator++() {ptr_++; return ptr_; } - T& operator*() { return *ptr_; } - T* operator->() { return ptr_; } - bool operator==(const iterator& other) {return ptr_ == other.ptr_; } - bool operator!=(const iterator& other) {return ptr_ != other.ptr_; } - - private: - T* ptr_; - }; - - list(){ - size_ = 0; - data_ = NULL; - }; - - /* Constructs class with passed in element. */ - list(T element){ - size_ = 1; - - T * data_ = new T [size_]; - - std::memcpy(data_, &element, sizeof(T)); - }; - - /* Constructs class with the passed in initializer_list. */ - list(std::initializer_list init_list){ - size_ = init_list.size(); - - T * temp = new T[size_]; - - std::memcpy(temp, std::begin(init_list), init_list.size()*sizeof(T)); - - data_ = temp; - } - - /* Returns the element at the front of the list. */ - T front(){ - if(size_ == 0){ - return T(); - } - return data_[0]; - } - - /* Returns the element at the back of the list. */ - T back(){ - if(size_ == 0){ - return T(); - } - return data_[size_ - 1]; - } - - /* Returns an iterator pointing at the front of the list. */ - iterator begin(){ - return iterator(data_); - } - - /* Returns an iterator pointing at the back of the list. */ - iterator end(){ - return iterator(data_ + size_); - } - - /* Inserts the passed in element at the passed in - * iterator position. - */ - iterator insert(iterator position, const T& element){ - int previous_size = size_; - size_++; - T *temp = new T[size_]; - - /* Counter tracks the index of the passed in iterator position. */ - int counter = 0; - for(auto iter = begin(); iter != end(); ++iter){ - if(iter == position){ - break; - } - counter++; - } - - int insert_index = counter; - int next_index = counter+1; - int last_index = previous_size; - int remaining_elements = previous_size - counter; - - /* Copy the original data that comes before the insertion. */ - std::memcpy(temp, data_, insert_index*sizeof(T)); - /* Copy the inserted data. */ - std::memcpy(temp + insert_index, &element, sizeof(T)); - /* Copy the original data that comes after the insertion. */ - std::memcpy(temp + next_index, data_+counter , remaining_elements*sizeof(T)); - - T *old_data = data_; - - data_ = temp; - - auto iter_pos = begin(); - /* Iterate until the inserted element. */ - for(int i = 0; i < insert_index; i++){ - ++iter_pos; - } - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - /* Return an iterator at the inserted element. */ - return iter_pos; - } - - /* Insert an element at the end of the list. */ - void push_back(T element){ - int previous_size = size_; - size_++; - - T *temp = new T[size_]; - - std::memcpy(temp, data_, previous_size*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - data_[previous_size] = element; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - }; - - /* Insert an element at the front of the list. */ - void push_front(T element){ - int previous_size = size_; - size_++; - - T *temp = new T[size_]; - - /* Copy the original data, leaving space for one element - * at the front. */ - std::memcpy(temp + 1, data_, previous_size*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - data_[0] = element; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - }; - - /* Returns the last element and removes it from the list. */ - T pop_back(){ - T back_elem = data_[size_ - 1]; - size_--; - - T *temp = new T[size_]; - - /* Copy the original data.*/ - std::memcpy(temp, data_, size_*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - return back_elem; - } - - /* Returns the first element and removes it from the list. */ - T pop_front(){ - T front_elem = front(); - size_--; - - T *temp = new T[size_]; - - /* Copy the original data.*/ - std::memcpy(temp, data_ + 1, size_*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - return front_elem; - } - - /* Returns the number of elements in the list. */ - int size(){ - return size_; - } - - void operator=(const list& other_list){ - data_ = other_list.data_; - size_ = other_list.size_; - } - - T& operator[](int index){ - return data_[index]; - } - - private: - T* data_; - size_t size_; - }; - -} -OPENTELEMETRY_END_NAMESPACE +/* Non-standard implementation of the list data structure. */ +template +class list +{ +public: + typedef T value_type; + + /* Custom iterator class*/ + class iterator + { + public: + typedef std::forward_iterator_tag iterator_category; + iterator(T *ptr) : ptr_(ptr) {} + iterator operator++() + { + ptr_++; + return ptr_; + } + T &operator*() { return *ptr_; } + T *operator->() { return ptr_; } + bool operator==(const iterator &other) { return ptr_ == other.ptr_; } + bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } + + private: + T *ptr_; + }; + + list() + { + size_ = 0; + data_ = NULL; + }; + + /* Constructs class with passed in element. */ + list(T element) + { + size_ = 1; + + T *data_ = new T[size_]; + + std::memcpy(data_, &element, sizeof(T)); + }; + + /* Constructs class with the passed in initializer_list. */ + list(std::initializer_list init_list) + { + size_ = init_list.size(); + + T *temp = new T[size_]; + + std::memcpy(temp, std::begin(init_list), init_list.size() * sizeof(T)); + + data_ = temp; + } + + /* Returns the element at the front of the list. */ + T front() + { + if (size_ == 0) + { + return T(); + } + return data_[0]; + } + + /* Returns the element at the back of the list. */ + T back() + { + if (size_ == 0) + { + return T(); + } + return data_[size_ - 1]; + } + + /* Returns an iterator pointing at the front of the list. */ + iterator begin() { return iterator(data_); } + + /* Returns an iterator pointing at the back of the list. */ + iterator end() { return iterator(data_ + size_); } + + /* Inserts the passed in element at the passed in + * iterator position. + */ + iterator insert(iterator position, const T &element) + { + int previous_size = size_; + size_++; + T *temp = new T[size_]; + + /* Counter tracks the index of the passed in iterator position. */ + int counter = 0; + for (auto iter = begin(); iter != end(); ++iter) + { + if (iter == position) + { + break; + } + counter++; + } + + int insert_index = counter; + int next_index = counter + 1; + int last_index = previous_size; + int remaining_elements = previous_size - counter; + + /* Copy the original data that comes before the insertion. */ + std::memcpy(temp, data_, insert_index * sizeof(T)); + /* Copy the inserted data. */ + std::memcpy(temp + insert_index, &element, sizeof(T)); + /* Copy the original data that comes after the insertion. */ + std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); + + T *old_data = data_; + + data_ = temp; + + auto iter_pos = begin(); + /* Iterate until the inserted element. */ + for (int i = 0; i < insert_index; i++) + { + ++iter_pos; + } + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + /* Return an iterator at the inserted element. */ + return iter_pos; + } + + /* Insert an element at the end of the list. */ + void push_back(T element) + { + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + std::memcpy(temp, data_, previous_size * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + data_[previous_size] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Insert an element at the front of the list. */ + void push_front(T element) + { + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + /* Copy the original data, leaving space for one element + * at the front. */ + std::memcpy(temp + 1, data_, previous_size * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + data_ = temp; + data_[0] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Returns the last element and removes it from the list. */ + T pop_back() + { + T back_elem = data_[size_ - 1]; + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_, size_ * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return back_elem; + } + + /* Returns the first element and removes it from the list. */ + T pop_front() + { + T front_elem = front(); + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_ + 1, size_ * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return front_elem; + } + + /* Returns the number of elements in the list. */ + int size() { return size_; } + + void operator=(const list &other_list) + { + data_ = other_list.data_; + size_ = other_list.size_; + } + + T &operator[](int index) { return data_[index]; } + +private: + T *data_; + size_t size_; +}; +} // namespace nostd +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index bf80805c30..58b777ae72 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -3,49 +3,45 @@ #include "opentelemetry/nostd/utility.h" #include "opentelemetry/version.h" - OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { -/* The nostd::pair class provides a data structure capable of hold - * two values of different or the same types, and provides access to each. +/* The nostd::pair class provides a data structure capable of hold + * two values of different or the same types, and provides access to each. */ template -class pair { - public: - - pair(){ - first_ = T1(); - second_ = T2(); - } - - /* Constructs a pair object with the passed in first and second values. */ - pair( T1 first, T2 second){ - first_ = first; - second_ = second; - } - - void operator=( const pair& other_pair){ - first_ = other_pair.first_; - second_ = other_pair.second_; - } - - /* Returns the first element. */ - T1 first(){ - return first_; - } - - /* Returns the second element. */ - T2 second(){ - return second_; - } - - private: - T1 first_; - T2 second_; - +class pair +{ +public: + pair() + { + first_ = T1(); + second_ = T2(); + } + + /* Constructs a pair object with the passed in first and second values. */ + pair(T1 first, T2 second) + { + first_ = first; + second_ = second; + } + + void operator=(const pair &other_pair) + { + first_ = other_pair.first_; + second_ = other_pair.second_; + } + + /* Returns the first element. */ + T1 first() { return first_; } + + /* Returns the second element. */ + T2 second() { return second_; } + +private: + T1 first_; + T2 second_; }; - -} +} // namespace nostd OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index b75962e4b6..ce84b20c6f 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -6,8 +6,8 @@ using opentelemetry::nostd::list; using opentelemetry::nostd::pair; -/* Tests that front() returns the first element and back() - * returns the last element. +/* Tests that front() returns the first element and back() + * returns the last element. */ TEST(ListTest, FrontBack) { @@ -20,20 +20,19 @@ TEST(ListTest, FrontBack) EXPECT_EQ(list_1.front(), 9); EXPECT_EQ(list_1.back(), 6); - } -/* Tests that the list object can handle data types of a size +/* Tests that the list object can handle data types of a size * other than an int. */ TEST(ListTest, PairList) { - list> pair_list; - - pair_list.push_back(pair(1,2)); - pair_list.push_back(pair(2,3)); - pair_list.push_back(pair(4,5)); + list> pair_list; + + pair_list.push_back(pair(1, 2)); + pair_list.push_back(pair(2, 3)); + pair_list.push_back(pair(4, 5)); - pair temp_pair; + pair temp_pair; temp_pair = pair_list.front(); EXPECT_EQ(pair_list.front().first(), 1); @@ -47,7 +46,7 @@ TEST(ListTest, PairList) TEST(ListTest, InitializerList) { - list pair_list = {1,2,3,4,5,6,7,8,9,10}; + list pair_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; EXPECT_EQ(pair_list.front(), 1); EXPECT_EQ(pair_list.back(), 10); @@ -57,72 +56,77 @@ TEST(ListTest, InitializerList) TEST(ListTest, IteratorInsertion) { - list int_list = {1,2,3,4}; - list insert_list = {5,6,7,8}; + list int_list = {1, 2, 3, 4}; + list insert_list = {5, 6, 7, 8}; std::insert_iterator> back(int_list, std::end(int_list)); - + for (auto iter = std::begin(insert_list); iter != std::end(insert_list); ++iter) { back = *iter; } - - int counter = 1; - for(auto iter = std::begin(int_list); iter != std::end(int_list); ++iter){ - EXPECT_EQ(*iter,counter); + + int counter = 1; + for (auto iter = std::begin(int_list); iter != std::end(int_list); ++iter) + { + EXPECT_EQ(*iter, counter); counter++; } } /* Tests that the push_front method added elements to the front of the list. */ -TEST(ListTest, PushFront){ +TEST(ListTest, PushFront) +{ list> list_list; - list inner_list = {0,1,2,3,4}; - list mid_list = {9,9,9,9,9}; + list inner_list = {0, 1, 2, 3, 4}; + list mid_list = {9, 9, 9, 9, 9}; list_list.push_front(inner_list); list_list.push_back(mid_list); list_list.push_front(inner_list); - EXPECT_EQ(list_list.front().front(),0); - EXPECT_EQ(list_list.front().back(),4); + EXPECT_EQ(list_list.front().front(), 0); + EXPECT_EQ(list_list.front().back(), 4); EXPECT_EQ(list_list.back().front(), 9); EXPECT_EQ(list_list.back().back(), 9); } /* Tests that you can modify elements via the subscript operator. */ -TEST(ListTest, SubscriptOperator){ - list int_list = {0,1,2,3,4}; - - EXPECT_EQ(int_list[2],2); +TEST(ListTest, SubscriptOperator) +{ + list int_list = {0, 1, 2, 3, 4}; + + EXPECT_EQ(int_list[2], 2); int_list[2] = 9; - EXPECT_EQ(int_list[2],9); + EXPECT_EQ(int_list[2], 9); } /* Tests that the pop_back method, returns and removes the back element - * from the list. + * from the list. */ -TEST(ListTest, PopBack){ - list int_list = {0,1,2,3,4}; - - int previous_size = int_list.size(); - EXPECT_EQ(int_list.back(),4); +TEST(ListTest, PopBack) +{ + list int_list = {0, 1, 2, 3, 4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.back(), 4); EXPECT_EQ(int_list.pop_back(), 4); EXPECT_EQ(int_list.size(), previous_size - 1); - + EXPECT_EQ(int_list.back(), 3); } /* Tests that the pop_front method, returns and removes the front element - * from the list. + * from the list. */ -TEST(ListTest, PopFront){ - list int_list = {0,1,2,3,4}; - - int previous_size = int_list.size(); - EXPECT_EQ(int_list.front(),0); +TEST(ListTest, PopFront) +{ + list int_list = {0, 1, 2, 3, 4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.front(), 0); EXPECT_EQ(int_list.pop_front(), 0); EXPECT_EQ(int_list.size(), previous_size - 1); - + EXPECT_EQ(int_list.front(), 1); } diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index ad2dc379f3..cccaf40092 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -7,11 +7,10 @@ using opentelemetry::nostd::pair; /* Tests constructing a pair object. */ TEST(PairTest, BasicConstruction) { - int val_1 = 1; - int val_2 = 9; - pair temp_pair = pair(val_1,val_2); + int val_1 = 1; + int val_2 = 9; + pair temp_pair = pair(val_1, val_2); EXPECT_EQ(temp_pair.first(), val_1); EXPECT_EQ(temp_pair.second(), val_2); - } From 821bfb433c49d15d983ad608b3a20c71c8447575 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 24 Jun 2020 19:27:47 +0000 Subject: [PATCH 3/8] added tests to build file --- api/test/nostd/BUILD | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/api/test/nostd/BUILD b/api/test/nostd/BUILD index 35306e6a3c..e816b63dba 100644 --- a/api/test/nostd/BUILD +++ b/api/test/nostd/BUILD @@ -74,3 +74,25 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "list_test", + srcs = [ + "list_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "pair_test", + srcs = [ + "pair_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) From 25813c8932d2beadcea6ed684a7f3583f59e3d15 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 25 Jun 2020 03:04:20 +0000 Subject: [PATCH 4/8] Added tests, removed unnecessary code and simplified things --- api/include/opentelemetry/nostd/list.h | 135 ++++++++++++------------- api/include/opentelemetry/nostd/pair.h | 22 ++-- api/test/nostd/list_test.cc | 91 ++++++++++++----- api/test/nostd/pair_test.cc | 2 +- 4 files changed, 148 insertions(+), 102 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index 1d6e48f063..352aa84013 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "opentelemetry/nostd/utility.h" @@ -12,18 +13,17 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { -/* Non-standard implementation of the list data structure. */ +// Non-standard implementation of the list data structure. template class list { public: typedef T value_type; - /* Custom iterator class*/ + // Custom iterator class class iterator { public: - typedef std::forward_iterator_tag iterator_category; iterator(T *ptr) : ptr_(ptr) {} iterator operator++() { @@ -36,26 +36,27 @@ class list bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } private: + friend class list; T *ptr_; }; list() { size_ = 0; - data_ = NULL; + data_ = nullptr; }; - /* Constructs class with passed in element. */ + // Constructs class with passed in element. list(T element) { size_ = 1; - T *data_ = new T[size_]; + data_ = new T[size_]; std::memcpy(data_, &element, sizeof(T)); }; - /* Constructs class with the passed in initializer_list. */ + // Constructs class with the passed in initializer_list. list(std::initializer_list init_list) { size_ = init_list.size(); @@ -67,83 +68,61 @@ class list data_ = temp; } - /* Returns the element at the front of the list. */ - T front() + // Returns the element at the front of the list. + T& front() { - if (size_ == 0) - { - return T(); - } return data_[0]; } - /* Returns the element at the back of the list. */ - T back() + // Returns the element at the back of the list. + T& back() { + if (size_ == 0) { - return T(); + return data_[0]; } + return data_[size_ - 1]; } - /* Returns an iterator pointing at the front of the list. */ + // Returns an iterator pointing at the front of the list. iterator begin() { return iterator(data_); } - /* Returns an iterator pointing at the back of the list. */ + // Returns an iterator pointing at the back of the list. iterator end() { return iterator(data_ + size_); } - /* Inserts the passed in element at the passed in - * iterator position. - */ + // Inserts the passed in element at the passed in + // iterator position. iterator insert(iterator position, const T &element) { int previous_size = size_; size_++; T *temp = new T[size_]; - /* Counter tracks the index of the passed in iterator position. */ - int counter = 0; - for (auto iter = begin(); iter != end(); ++iter) - { - if (iter == position) - { - break; - } - counter++; - } + // Counter tracks the index of the passed in iterator position. + int counter = position.ptr_ - data_; int insert_index = counter; int next_index = counter + 1; - int last_index = previous_size; int remaining_elements = previous_size - counter; - /* Copy the original data that comes before the insertion. */ + // Copy the original data that comes before the insertion. std::memcpy(temp, data_, insert_index * sizeof(T)); - /* Copy the inserted data. */ + // Copy the inserted data. std::memcpy(temp + insert_index, &element, sizeof(T)); - /* Copy the original data that comes after the insertion. */ + // Copy the original data that comes after the insertion. std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); - T *old_data = data_; - + delete[] data_; + data_ = temp; - auto iter_pos = begin(); - /* Iterate until the inserted element. */ - for (int i = 0; i < insert_index; i++) - { - ++iter_pos; - } - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - /* Return an iterator at the inserted element. */ - return iter_pos; + // Return an iterator at the inserted element. + return iterator(data_ + counter); } - /* Insert an element at the end of the list. */ + // Insert an element at the end of the list. void push_back(T element) { int previous_size = size_; @@ -153,18 +132,16 @@ class list std::memcpy(temp, data_, previous_size * sizeof(T)); - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - + // Deallocate memory used by previous array. + delete[] data_; + data_ = temp; data_[previous_size] = element; - /* Deallocate memory used by previous array. */ - delete[] old_data; }; - /* Insert an element at the front of the list. */ + // Insert an element at the front of the list. void push_front(T element) { int previous_size = size_; @@ -172,44 +149,45 @@ class list T *temp = new T[size_]; - /* Copy the original data, leaving space for one element - * at the front. */ + // Copy the original data, leaving space for one element + // at the front. std::memcpy(temp + 1, data_, previous_size * sizeof(T)); - /* Save old array pointer for later deallocation. */ + // Save old array pointer for later deallocation. T *old_data = data_; data_ = temp; data_[0] = element; - /* Deallocate memory used by previous array. */ + // Deallocate memory used by previous array. delete[] old_data; }; - /* Returns the last element and removes it from the list. */ + // Returns the last element and removes it from the list. T pop_back() { + T back_elem = data_[size_ - 1]; size_--; T *temp = new T[size_]; - /* Copy the original data.*/ + // Copy the original data. std::memcpy(temp, data_, size_ * sizeof(T)); - /* Save old array pointer for later deallocation. */ + // Save old array pointer for later deallocation. T *old_data = data_; data_ = temp; - /* Deallocate memory used by previous array. */ + // Deallocate memory used by previous array. delete[] old_data; return back_elem; } - /* Returns the first element and removes it from the list. */ + // Returns the first element and removes it from the list. T pop_front() { T front_elem = front(); @@ -217,29 +195,46 @@ class list T *temp = new T[size_]; - /* Copy the original data.*/ + // Copy the original data. std::memcpy(temp, data_ + 1, size_ * sizeof(T)); - /* Save old array pointer for later deallocation. */ + // Save old array pointer for later deallocation. T *old_data = data_; data_ = temp; - /* Deallocate memory used by previous array. */ + // Deallocate memory used by previous array. delete[] old_data; return front_elem; } - /* Returns the number of elements in the list. */ + // Returns the number of elements in the list. // int size() { return size_; } void operator=(const list &other_list) { - data_ = other_list.data_; + delete[] data_; + data_ = new T[other_list.size_]; + memcpy(data_, other_list.data_, other_list.size_*sizeof(T)); size_ = other_list.size_; } + + // Returns true if the lists are the same, false if they are not + bool operator==(const list &other_list) + { + if(size_ != other_list.size_){ + return false; + } + if( memcmp(data_, other_list.data_, size_) == 0){ + return true; + } + else{ + return false; + } + } + T &operator[](int index) { return data_[index]; } private: diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index 58b777ae72..cf17f393e4 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -7,9 +7,9 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { -/* The nostd::pair class provides a data structure capable of hold - * two values of different or the same types, and provides access to each. - */ +// The nostd::pair class provides a data structure capable of hold +// two values of different or the same types, and provides access to each. + template class pair { @@ -20,7 +20,7 @@ class pair second_ = T2(); } - /* Constructs a pair object with the passed in first and second values. */ + // Constructs a pair object with the passed in first and second values. pair(T1 first, T2 second) { first_ = first; @@ -33,10 +33,20 @@ class pair second_ = other_pair.second_; } - /* Returns the first element. */ + bool operator==(const pair &other_pair) + { + if(first_ == other_pair.first_){ + if(second_ == other_pair.second_){ + return true; + } + } + return false; + } + + // Returns the first element. T1 first() { return first_; } - /* Returns the second element. */ + // Returns the second element. T2 second() { return second_; } private: diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index ce84b20c6f..36106d5fda 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -2,14 +2,15 @@ #include "opentelemetry/nostd/pair.h" #include +#include using opentelemetry::nostd::list; using opentelemetry::nostd::pair; -/* Tests that front() returns the first element and back() - * returns the last element. - */ -TEST(ListTest, FrontBack) +// Tests that front() returns the first element and back() +// returns the last element. + +TEST(ListTest, FrontBackReturnsProperElements) { list list_1; @@ -22,9 +23,9 @@ TEST(ListTest, FrontBack) EXPECT_EQ(list_1.back(), 6); } -/* Tests that the list object can handle data types of a size - * other than an int. */ -TEST(ListTest, PairList) +// Tests that the list object can handle data types of a size +// other than an int. +TEST(ListTest, ListAcceptsPairDatatype) { list> pair_list; @@ -42,8 +43,8 @@ TEST(ListTest, PairList) EXPECT_EQ(pair_list.back().second(), 5); } -/* Tests taht a list object can be initialized with an initalizer list. */ -TEST(ListTest, InitializerList) +// Tests that a list object can be initialized with an initalizer list. +TEST(ListTest, ListConstructorAcceptsInitializerList) { list pair_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; @@ -52,8 +53,8 @@ TEST(ListTest, InitializerList) EXPECT_EQ(pair_list.back(), 10); } -/* Tests that the list object can handle an insert_iterator. */ -TEST(ListTest, IteratorInsertion) +// Tests that the list object can handle an insert_iterator. +TEST(ListTest, InsertIteratorAddsToList) { list int_list = {1, 2, 3, 4}; @@ -72,14 +73,16 @@ TEST(ListTest, IteratorInsertion) EXPECT_EQ(*iter, counter); counter++; } + } -/* Tests that the push_front method added elements to the front of the list. */ -TEST(ListTest, PushFront) +// Tests that the push_front method added elements to the front of the list. +TEST(ListTest, PushFrontAddsDataAtFront) { list> list_list; list inner_list = {0, 1, 2, 3, 4}; - list mid_list = {9, 9, 9, 9, 9}; + list mid_list = {5, 6, 7, 8, 9}; + list_list.push_front(inner_list); list_list.push_back(mid_list); list_list.push_front(inner_list); @@ -87,12 +90,12 @@ TEST(ListTest, PushFront) EXPECT_EQ(list_list.front().front(), 0); EXPECT_EQ(list_list.front().back(), 4); - EXPECT_EQ(list_list.back().front(), 9); + EXPECT_EQ(list_list.back().front(), 5); EXPECT_EQ(list_list.back().back(), 9); } -/* Tests that you can modify elements via the subscript operator. */ -TEST(ListTest, SubscriptOperator) +// Tests that you can modify elements via the subscript operator. +TEST(ListTest, SubscriptOperatorAllowsForValueModification) { list int_list = {0, 1, 2, 3, 4}; @@ -101,10 +104,9 @@ TEST(ListTest, SubscriptOperator) EXPECT_EQ(int_list[2], 9); } -/* Tests that the pop_back method, returns and removes the back element - * from the list. - */ -TEST(ListTest, PopBack) +// Tests that the pop_back method returns and removes the back element +// from the list. +TEST(ListTest, PopBackReturnsAndRemovesFromBack) { list int_list = {0, 1, 2, 3, 4}; @@ -116,10 +118,9 @@ TEST(ListTest, PopBack) EXPECT_EQ(int_list.back(), 3); } -/* Tests that the pop_front method, returns and removes the front element - * from the list. - */ -TEST(ListTest, PopFront) +// Tests that the pop_front method returns and removes the front element +// from the list. +TEST(ListTest, PopFrontReturnsAndRemovesFromFront) { list int_list = {0, 1, 2, 3, 4}; @@ -130,3 +131,43 @@ TEST(ListTest, PopFront) EXPECT_EQ(int_list.front(), 1); } + +// Tests that after using the assignment operator, changing the new +// doesn't change the original +TEST(ListTest,AssignmentOperatorDoesNotChangeOriginal) +{ + + list first_list = {0, 1, 2, 3, 4}; + list first_list_copy = {0, 1, 2, 3, 4}; + + list second_list = {5, 6, 7, 8, 9}; + second_list = first_list; + second_list.pop_back(); + second_list.pop_back(); + second_list.pop_back(); + + EXPECT_EQ(first_list == first_list_copy, true); + EXPECT_EQ(first_list == second_list, false); + +} + +// Tests that two lists with the same contents return true when compared +// using the comparison operator +TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents){ + + list first_list = {0, 1, 2, 3, 4}; + list second_list = {0, 1, 2, 3, 4}; + list third_list = {0, 1, 2, 3, 5}; + EXPECT_EQ(first_list == second_list, true); + EXPECT_NE(first_list == third_list, false); +} + +// Tests that constructing a list with one element populates the list object +// with that element +TEST(ListTest, SingleElementConstruction){ + list single_list = list(5); + + EXPECT_EQ(single_list[0], 5); +} + + diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index cccaf40092..e4a6fd7eb7 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -4,7 +4,7 @@ using opentelemetry::nostd::pair; -/* Tests constructing a pair object. */ +// Tests constructing a pair object. TEST(PairTest, BasicConstruction) { int val_1 = 1; From ec12e39f55ae02595657d1c425e5a8485cbed524 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 25 Jun 2020 03:23:56 +0000 Subject: [PATCH 5/8] added assignment tests for pair and comparison test for pair --- api/include/opentelemetry/nostd/pair.h | 4 +- api/test/nostd/pair_test.cc | 62 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index cf17f393e4..116661b28d 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -44,10 +44,10 @@ class pair } // Returns the first element. - T1 first() { return first_; } + T1 & first() { return first_; } // Returns the second element. - T2 second() { return second_; } + T2 & second() { return second_; } private: T1 first_; diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index e4a6fd7eb7..e8171b2190 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -14,3 +14,65 @@ TEST(PairTest, BasicConstruction) EXPECT_EQ(temp_pair.first(), val_1); EXPECT_EQ(temp_pair.second(), val_2); } + +// Tests that the comparison operator returns true when pair values +// are the same and returns false when different. +TEST(PairTest, ComparisonSameValuesReturnTrue){ + + int val_1 = 1; + int val_2 = 2; + pair pair_1 = pair(val_1, val_2); + pair pair_2 = pair(val_1, val_2); + + EXPECT_EQ(pair_1 == pair_2, true); + + pair_1.first() = 8; + + EXPECT_EQ(pair_1 == pair_2, false); +} + +// Tests that the assignment operator sets the values as one would expect. +TEST(PairTest, AssignmentBasic){ + + int val_1 = 1; + int val_2 = 2; + pair pair_1 = pair(val_1, val_2); + + int val_3 = 3; + int val_4 = 4; + pair pair_2 = pair(val_3, val_4); + + EXPECT_EQ(pair_1 == pair_2, false); + pair_1 = pair_2; + + EXPECT_EQ(pair_1 == pair_2, true); + EXPECT_EQ(pair_1.first(), val_3); + EXPECT_EQ(pair_1.second(), val_4); + +} + +// Tests that after using the assignment operator, changing values of one +// object does not change both. +TEST(PairTest, AssignmentOtherObjectImmutable){ + + int val_1 = 1; + int val_2 = 2; + pair pair_1 = pair(val_1, val_2); + + int val_3 = 3; + int val_4 = 4; + pair pair_2 = pair(val_3, val_4); + + pair_1 = pair_2; + + pair_1.first() = 8; + pair_1.second() = 7; + + EXPECT_EQ(pair_1.first(), 8); + EXPECT_EQ(pair_1.second(), 7); + + EXPECT_EQ(pair_2.first(), val_3); + EXPECT_EQ(pair_2.second(), val_4); +} + + From 7ac58e26382ff595d474bdc04e362ecf8f49c4c6 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 25 Jun 2020 03:32:12 +0000 Subject: [PATCH 6/8] moved some delete statements --- api/include/opentelemetry/nostd/list.h | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index 352aa84013..bf90c9a7b2 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -153,15 +153,11 @@ class list // at the front. std::memcpy(temp + 1, data_, previous_size * sizeof(T)); - // Save old array pointer for later deallocation. - T *old_data = data_; + delete[] data_; data_ = temp; data_[0] = element; - - // Deallocate memory used by previous array. - delete[] old_data; }; // Returns the last element and removes it from the list. @@ -176,14 +172,10 @@ class list // Copy the original data. std::memcpy(temp, data_, size_ * sizeof(T)); - // Save old array pointer for later deallocation. - T *old_data = data_; + delete[] data_; data_ = temp; - // Deallocate memory used by previous array. - delete[] old_data; - return back_elem; } @@ -198,13 +190,10 @@ class list // Copy the original data. std::memcpy(temp, data_ + 1, size_ * sizeof(T)); - // Save old array pointer for later deallocation. - T *old_data = data_; + delete[] data_; data_ = temp; - // Deallocate memory used by previous array. - delete[] old_data; return front_elem; } @@ -234,7 +223,7 @@ class list return false; } } - + T &operator[](int index) { return data_[index]; } private: From cf403b7070475ce187b2ed308c16a88b2cd2c76c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Fri, 26 Jun 2020 19:29:35 +0000 Subject: [PATCH 7/8] fixed memory leak in list and pair --- api/include/opentelemetry/nostd/list.h | 96 +++++++++++++++---------- api/include/opentelemetry/nostd/pair.h | 11 +-- api/test/nostd/list_test.cc | 99 ++++++++++++++------------ api/test/nostd/pair_test.cc | 64 +++++++++++------ 4 files changed, 155 insertions(+), 115 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index bf90c9a7b2..59705bb0c4 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -3,8 +3,8 @@ #include #include #include -#include #include +#include #include #include "opentelemetry/nostd/utility.h" @@ -24,19 +24,25 @@ class list class iterator { public: - iterator(T *ptr) : ptr_(ptr) {} iterator operator++() { ptr_++; return ptr_; } + T &operator*() { return *ptr_; } + T *operator->() { return ptr_; } + bool operator==(const iterator &other) { return ptr_ == other.ptr_; } + bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } private: friend class list; + + iterator(T *ptr) : ptr_(ptr) {} + T *ptr_; }; @@ -61,28 +67,22 @@ class list { size_ = init_list.size(); - T *temp = new T[size_]; - - std::memcpy(temp, std::begin(init_list), init_list.size() * sizeof(T)); + data_ = new T[size_]; - data_ = temp; + std::memcpy(data_, std::begin(init_list), init_list.size() * sizeof(T)); } // Returns the element at the front of the list. - T& front() - { - return data_[0]; - } + T &front() { return data_[0]; } // Returns the element at the back of the list. - T& back() + T &back() { - if (size_ == 0) { return data_[0]; } - + return data_[size_ - 1]; } @@ -93,11 +93,12 @@ class list iterator end() { return iterator(data_ + size_); } // Inserts the passed in element at the passed in - // iterator position. + // iterator position. iterator insert(iterator position, const T &element) { int previous_size = size_; size_++; + T *temp = new T[size_]; // Counter tracks the index of the passed in iterator position. @@ -115,7 +116,7 @@ class list std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); delete[] data_; - + data_ = temp; // Return an iterator at the inserted element. @@ -125,45 +126,50 @@ class list // Insert an element at the end of the list. void push_back(T element) { + int previous_size = size_; size_++; T *temp = new T[size_]; - std::memcpy(temp, data_, previous_size * sizeof(T)); + // Copy the original data into the new array. + for (int i = 0; i < previous_size; i++) + { + temp[i] = data_[i]; + } // Deallocate memory used by previous array. delete[] data_; - + data_ = temp; data_[previous_size] = element; - }; // Insert an element at the front of the list. void push_front(T element) { - int previous_size = size_; size_++; T *temp = new T[size_]; - // Copy the original data, leaving space for one element - // at the front. - std::memcpy(temp + 1, data_, previous_size * sizeof(T)); + // Copy the original data into the new array leaving space + // for the new element at the front. + for (unsigned int i = 1; i < size_; i++) + { + temp[i] = data_[i - 1]; + } + // Deallocate memory used by previous array. delete[] data_; - data_ = temp; - + data_ = temp; data_[0] = element; }; // Returns the last element and removes it from the list. T pop_back() { - T back_elem = data_[size_ - 1]; size_--; @@ -172,6 +178,7 @@ class list // Copy the original data. std::memcpy(temp, data_, size_ * sizeof(T)); + // Deallocate memory used by previous array. delete[] data_; data_ = temp; @@ -190,45 +197,58 @@ class list // Copy the original data. std::memcpy(temp, data_ + 1, size_ * sizeof(T)); + // Deallocate memory used by previous array. delete[] data_; data_ = temp; - return front_elem; } // Returns the number of elements in the list. // int size() { return size_; } - void operator=(const list &other_list) + list &operator=(const list &other_list) { delete[] data_; + data_ = new T[other_list.size_]; - memcpy(data_, other_list.data_, other_list.size_*sizeof(T)); + + memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); + size_ = other_list.size_; + + return *this; } - - // Returns true if the lists are the same, false if they are not + + list(const list &other_list) + { + size_ = other_list.size_; + + data_ = new T[size_]; + + memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); + } + + // Returns true if the list contents are the same, false if they are not bool operator==(const list &other_list) { - if(size_ != other_list.size_){ + if (size_ != other_list.size_) + { return false; } - if( memcmp(data_, other_list.data_, size_) == 0){ - return true; - } - else{ - return false; - } + return (memcmp(data_, other_list.data_, size_) == 0); } T &operator[](int index) { return data_[index]; } + ~list() { delete[] data_; } + private: - T *data_; size_t size_; + + T *data_; }; } // namespace nostd OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index 116661b28d..87dd6f03f2 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -35,19 +35,14 @@ class pair bool operator==(const pair &other_pair) { - if(first_ == other_pair.first_){ - if(second_ == other_pair.second_){ - return true; - } - } - return false; + return (first_ == other_pair.first_ && second_ == other_pair.second_); } // Returns the first element. - T1 & first() { return first_; } + T1 &first() { return first_; } // Returns the second element. - T2 & second() { return second_; } + T2 &second() { return second_; } private: T1 first_; diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index 36106d5fda..4ede6fc2b1 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -1,30 +1,29 @@ #include "opentelemetry/nostd/list.h" #include "opentelemetry/nostd/pair.h" -#include #include +#include using opentelemetry::nostd::list; using opentelemetry::nostd::pair; - // Tests that front() returns the first element and back() // returns the last element. - + TEST(ListTest, FrontBackReturnsProperElements) { - list list_1; + list int_list; - list_1.push_back(9); - list_1.push_back(8); - list_1.push_back(7); - list_1.push_back(6); + int_list.push_back(9); + int_list.push_back(8); + int_list.push_back(7); + int_list.push_back(6); - EXPECT_EQ(list_1.front(), 9); - EXPECT_EQ(list_1.back(), 6); + EXPECT_EQ(int_list.front(), 9); + EXPECT_EQ(int_list.back(), 6); } // Tests that the list object can handle data types of a size -// other than an int. +// other than an int. TEST(ListTest, ListAcceptsPairDatatype) { list> pair_list; @@ -33,12 +32,8 @@ TEST(ListTest, ListAcceptsPairDatatype) pair_list.push_back(pair(2, 3)); pair_list.push_back(pair(4, 5)); - pair temp_pair; - temp_pair = pair_list.front(); - EXPECT_EQ(pair_list.front().first(), 1); EXPECT_EQ(pair_list.front().second(), 2); - EXPECT_EQ(pair_list.back().first(), 4); EXPECT_EQ(pair_list.back().second(), 5); } @@ -46,17 +41,15 @@ TEST(ListTest, ListAcceptsPairDatatype) // Tests that a list object can be initialized with an initalizer list. TEST(ListTest, ListConstructorAcceptsInitializerList) { + list int_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - list pair_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - - EXPECT_EQ(pair_list.front(), 1); - EXPECT_EQ(pair_list.back(), 10); + EXPECT_EQ(int_list.front(), 1); + EXPECT_EQ(int_list.back(), 10); } // Tests that the list object can handle an insert_iterator. TEST(ListTest, InsertIteratorAddsToList) { - list int_list = {1, 2, 3, 4}; list insert_list = {5, 6, 7, 8}; @@ -68,12 +61,12 @@ TEST(ListTest, InsertIteratorAddsToList) } int counter = 1; + for (auto iter = std::begin(int_list); iter != std::end(int_list); ++iter) { EXPECT_EQ(*iter, counter); counter++; } - } // Tests that the push_front method added elements to the front of the list. @@ -82,16 +75,34 @@ TEST(ListTest, PushFrontAddsDataAtFront) list> list_list; list inner_list = {0, 1, 2, 3, 4}; list mid_list = {5, 6, 7, 8, 9}; - - list_list.push_front(inner_list); - list_list.push_back(mid_list); + list end_list = {10, 11, 12, 13, 14}; + + list_list.push_front(end_list); + list_list.push_front(mid_list); list_list.push_front(inner_list); EXPECT_EQ(list_list.front().front(), 0); EXPECT_EQ(list_list.front().back(), 4); + EXPECT_EQ(list_list.back().front(), 10); + EXPECT_EQ(list_list.back().back(), 14); +} - EXPECT_EQ(list_list.back().front(), 5); - EXPECT_EQ(list_list.back().back(), 9); +// Tests that the push_back method added elements at the back of the list. +TEST(ListTest, PushBackAddsDataAtBack) +{ + list> list_list; + list inner_list = {0, 1, 2, 3, 4}; + list mid_list = {5, 6, 7, 8, 9}; + list end_list = {10, 11, 12, 13, 14}; + + list_list.push_back(inner_list); + list_list.push_back(mid_list); + list_list.push_back(end_list); + + EXPECT_EQ(list_list.front().front(), 0); + EXPECT_EQ(list_list.front().back(), 4); + EXPECT_EQ(list_list.back().front(), 10); + EXPECT_EQ(list_list.back().back(), 14); } // Tests that you can modify elements via the subscript operator. @@ -109,12 +120,11 @@ TEST(ListTest, SubscriptOperatorAllowsForValueModification) TEST(ListTest, PopBackReturnsAndRemovesFromBack) { list int_list = {0, 1, 2, 3, 4}; + int previous_size = int_list.size(); - int previous_size = int_list.size(); EXPECT_EQ(int_list.back(), 4); EXPECT_EQ(int_list.pop_back(), 4); EXPECT_EQ(int_list.size(), previous_size - 1); - EXPECT_EQ(int_list.back(), 3); } @@ -123,51 +133,48 @@ TEST(ListTest, PopBackReturnsAndRemovesFromBack) TEST(ListTest, PopFrontReturnsAndRemovesFromFront) { list int_list = {0, 1, 2, 3, 4}; + int previous_size = int_list.size(); - int previous_size = int_list.size(); EXPECT_EQ(int_list.front(), 0); EXPECT_EQ(int_list.pop_front(), 0); EXPECT_EQ(int_list.size(), previous_size - 1); - EXPECT_EQ(int_list.front(), 1); } -// Tests that after using the assignment operator, changing the new +// Tests that after using the assignment operator, changing the new // doesn't change the original -TEST(ListTest,AssignmentOperatorDoesNotChangeOriginal) +TEST(ListTest, AssignmentOperatorDoesNotChangeOriginal) { - list first_list = {0, 1, 2, 3, 4}; + list first_list = {0, 1, 2, 3, 4}; list first_list_copy = {0, 1, 2, 3, 4}; - - list second_list = {5, 6, 7, 8, 9}; - second_list = first_list; + list second_list = first_list; + second_list.pop_back(); second_list.pop_back(); second_list.pop_back(); EXPECT_EQ(first_list == first_list_copy, true); EXPECT_EQ(first_list == second_list, false); - } -// Tests that two lists with the same contents return true when compared +// Tests that two lists with the same contents return true when compared // using the comparison operator -TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents){ - - list first_list = {0, 1, 2, 3, 4}; +TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents) +{ + list first_list = {0, 1, 2, 3, 4}; list second_list = {0, 1, 2, 3, 4}; - list third_list = {0, 1, 2, 3, 5}; + list third_list = {0, 1, 2, 3, 5}; + EXPECT_EQ(first_list == second_list, true); EXPECT_NE(first_list == third_list, false); } // Tests that constructing a list with one element populates the list object // with that element -TEST(ListTest, SingleElementConstruction){ +TEST(ListTest, SingleElementConstruction) +{ list single_list = list(5); - + EXPECT_EQ(single_list[0], 5); } - - diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index e8171b2190..342900a30e 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -1,7 +1,11 @@ #include "opentelemetry/nostd/pair.h" +#include "opentelemetry/nostd/list.h" + +#include #include +using opentelemetry::nostd::list; using opentelemetry::nostd::pair; // Tests constructing a pair object. @@ -17,62 +21,76 @@ TEST(PairTest, BasicConstruction) // Tests that the comparison operator returns true when pair values // are the same and returns false when different. -TEST(PairTest, ComparisonSameValuesReturnTrue){ +TEST(PairTest, ComparisonSameValuesReturnTrue) +{ - int val_1 = 1; - int val_2 = 2; + int val_1 = 1; + int val_2 = 2; pair pair_1 = pair(val_1, val_2); pair pair_2 = pair(val_1, val_2); EXPECT_EQ(pair_1 == pair_2, true); - + pair_1.first() = 8; - + EXPECT_EQ(pair_1 == pair_2, false); } // Tests that the assignment operator sets the values as one would expect. -TEST(PairTest, AssignmentBasic){ +TEST(PairTest, AssignmentBasic) +{ - int val_1 = 1; - int val_2 = 2; + int val_1 = 1; + int val_2 = 2; pair pair_1 = pair(val_1, val_2); - - int val_3 = 3; - int val_4 = 4; + + int val_3 = 3; + int val_4 = 4; pair pair_2 = pair(val_3, val_4); - EXPECT_EQ(pair_1 == pair_2, false); + // EXPECT_EQ(pair_1 == pair_2, false); + EXPECT_EQ(pair_1, pair_2); + pair_1 = pair_2; - + EXPECT_EQ(pair_1 == pair_2, true); EXPECT_EQ(pair_1.first(), val_3); EXPECT_EQ(pair_1.second(), val_4); - } -// Tests that after using the assignment operator, changing values of one +// Tests that after using the assignment operator, changing values of one // object does not change both. -TEST(PairTest, AssignmentOtherObjectImmutable){ +TEST(PairTest, AssignmentOtherObjectImmutable) +{ - int val_1 = 1; - int val_2 = 2; + int val_1 = 1; + int val_2 = 2; pair pair_1 = pair(val_1, val_2); - - int val_3 = 3; - int val_4 = 4; + + int val_3 = 3; + int val_4 = 4; pair pair_2 = pair(val_3, val_4); pair_1 = pair_2; - pair_1.first() = 8; + pair_1.first() = 8; pair_1.second() = 7; EXPECT_EQ(pair_1.first(), 8); EXPECT_EQ(pair_1.second(), 7); - EXPECT_EQ(pair_2.first(), val_3); EXPECT_EQ(pair_2.second(), val_4); } +// Tests that the pair class can accept containers +TEST(PairTest, PairAcceptContainers) +{ + list int_list = {1, 2, 3, 4}; + std::string test_string = "test"; + + pair, std::string> container_pair = pair, std::string>(int_list, test_string); + EXPECT_EQ(container_pair.second(), test_string); + EXPECT_EQ(container_pair.first().front(), int_list.front()); + EXPECT_EQ(container_pair.first().back(), int_list.back()); +} From 09b0d5083d222f7416aa6d84b91ec5ea7b9c4094 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Fri, 26 Jun 2020 19:47:07 +0000 Subject: [PATCH 8/8] fixed list comparison error --- api/include/opentelemetry/nostd/list.h | 4 ++-- api/test/nostd/list_test.cc | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index 59705bb0c4..770a9dc335 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -237,8 +237,8 @@ class list { return false; } - - return (memcmp(data_, other_list.data_, size_) == 0); + + return (memcmp(data_, other_list.data_, size_*sizeof(T)) == 0); } T &operator[](int index) { return data_[index]; } diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index 4ede6fc2b1..41cf8739df 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -6,9 +6,9 @@ using opentelemetry::nostd::list; using opentelemetry::nostd::pair; + // Tests that front() returns the first element and back() // returns the last element. - TEST(ListTest, FrontBackReturnsProperElements) { list int_list; @@ -154,8 +154,8 @@ TEST(ListTest, AssignmentOperatorDoesNotChangeOriginal) second_list.pop_back(); second_list.pop_back(); - EXPECT_EQ(first_list == first_list_copy, true); - EXPECT_EQ(first_list == second_list, false); + EXPECT_TRUE(first_list == first_list_copy); + EXPECT_FALSE(first_list == second_list); } // Tests that two lists with the same contents return true when compared @@ -166,8 +166,8 @@ TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents) list second_list = {0, 1, 2, 3, 4}; list third_list = {0, 1, 2, 3, 5}; - EXPECT_EQ(first_list == second_list, true); - EXPECT_NE(first_list == third_list, false); + EXPECT_TRUE(first_list == second_list); + EXPECT_FALSE(first_list == third_list); } // Tests that constructing a list with one element populates the list object