From df2480c73c881d3ecefb3cd05d80fa14b2c49e5e Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Fri, 14 Feb 2025 01:05:55 -0800 Subject: [PATCH] feat: complete chapter 6 lab 6 --- meson.build | 2 + src/chapter_6/lab-6/IntVector.cpp | 33 +++++++++++++++ src/chapter_6/lab-6/IntVector.h | 18 ++++++++ src/chapter_6/lab-6/main.cpp | 68 +++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/chapter_6/lab-6/IntVector.cpp create mode 100644 src/chapter_6/lab-6/IntVector.h create mode 100644 src/chapter_6/lab-6/main.cpp diff --git a/meson.build b/meson.build index 0e6b036..4339f9c 100644 --- a/meson.build +++ b/meson.build @@ -44,3 +44,5 @@ c_5_lab_5_1 = executable( ['src/chapter_5/lab_5-1/main.cpp'], install: true, ) + +c_6_lab_6 = executable('c_6_lab-6', ['src/chapter_6/lab-6/main.cpp', 'src/chapter_6/lab-6/IntVector.h', 'src/chapter_6/lab-6/IntVector.cpp'], install: true) diff --git a/src/chapter_6/lab-6/IntVector.cpp b/src/chapter_6/lab-6/IntVector.cpp new file mode 100644 index 0000000..3f07aeb --- /dev/null +++ b/src/chapter_6/lab-6/IntVector.cpp @@ -0,0 +1,33 @@ +#include "IntVector.h" +#include +using namespace std; + +IntVector::IntVector(unsigned capacity, int value) { + __size = capacity; + __capacity = capacity; + + if (__size > 0) { + __data = new int[__size]; + } else { + __data = nullptr; + } + + for (unsigned i = 0; i < __size; i++) { + __data[i] = value; + } +} + +IntVector::~IntVector() { delete[] __data; } + +const int &IntVector::at(unsigned index) const { + if (index >= __size) + throw out_of_range("IntVector::at_range_check"); + + return __data[index]; +} + +unsigned IntVector::size() const { return __size; } +unsigned IntVector::capacity() const { return __capacity; }; +bool IntVector::empty() const { return __size == 0; }; +const int &IntVector::front() const { return __data[0]; } +const int &IntVector::back() const { return __data[__size - 1]; } diff --git a/src/chapter_6/lab-6/IntVector.h b/src/chapter_6/lab-6/IntVector.h new file mode 100644 index 0000000..20d97e0 --- /dev/null +++ b/src/chapter_6/lab-6/IntVector.h @@ -0,0 +1,18 @@ +#pragma once + +class IntVector { +public: + IntVector(unsigned capacity = 0, int value = 0); + ~IntVector(); + unsigned size() const; + unsigned capacity() const; + bool empty() const; + const int &at(unsigned index) const; + const int &front() const; + const int &back() const; + +private: + unsigned __size; + unsigned __capacity; + int *__data; +}; diff --git a/src/chapter_6/lab-6/main.cpp b/src/chapter_6/lab-6/main.cpp new file mode 100644 index 0000000..92dd847 --- /dev/null +++ b/src/chapter_6/lab-6/main.cpp @@ -0,0 +1,68 @@ +#include "IntVector.h" +#include +#include +#include +using namespace std; + +int main() { + cout << "Running test bench!" << endl; + + IntVector *testVec = nullptr; + + testVec = new IntVector(); + assert(testVec->empty()); + cout << "[SUCCESS] empty() works as expected for empty vector." << endl; + assert(testVec->size() == 0); + assert(testVec->capacity() == 0); + cout << "[SUCCESS] Default constructor sets size and capacity properly." + << endl; + + testVec = new IntVector(1); + assert(!testVec->empty()); + cout << "[SUCCESS] empty() works as expected for nonempty vector." << endl; + assert(testVec->at(0) == 0); + cout << "[SUCCESS] Basic vector initialization works." << endl; + + for (unsigned i = 0; i < 100; i++) { + testVec = new IntVector(i, 50 - i); + + assert(testVec->capacity() == i); + assert(testVec->size() == i); + + for (unsigned j = 0; j < testVec->size(); j++) { + assert(testVec->at(j) == 50 - static_cast(i)); + } + } + + cout << "[SUCCESS] Size and capacity work as expected" << endl; + cout << "[SUCCESS] Initializing values getting values at indices work." + << endl; + + testVec = new IntVector(5, 2); + + bool failed = false; + try { + testVec->at(5); + } catch (const out_of_range &e) { + failed = true; + } + assert(failed); + cout << "[SUCCESS] at() correctly throws for out of range access." << endl; + + assert((testVec->front() == testVec->at(0))); + cout << "[SUCCESS] Output of front() is consistent with output of at(0)." + << endl; + + assert(testVec->front() == 2); + cout << "[SUCCESS] Output of front() is correct." << endl; + + assert((testVec->back() == testVec->at(testVec->size() - 1))); + cout + << "[SUCCESS] Output of back() is consistent with output of at(size - 1)." + << endl; + + assert(testVec->front() == 2); + cout << "[SUCCESS] Output of back() is correct." << endl; + + cout << "All tests passed!" << endl; +}