feat: complete chapter 6 lab 6
This commit is contained in:
parent
c281462187
commit
df2480c73c
4 changed files with 121 additions and 0 deletions
|
@ -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)
|
||||
|
|
33
src/chapter_6/lab-6/IntVector.cpp
Normal file
33
src/chapter_6/lab-6/IntVector.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include "IntVector.h"
|
||||
#include <stdexcept>
|
||||
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]; }
|
18
src/chapter_6/lab-6/IntVector.h
Normal file
18
src/chapter_6/lab-6/IntVector.h
Normal file
|
@ -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;
|
||||
};
|
68
src/chapter_6/lab-6/main.cpp
Normal file
68
src/chapter_6/lab-6/main.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "IntVector.h"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
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<int>(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;
|
||||
}
|
Loading…
Reference in a new issue