Skip to content

Commit 96df6fe

Browse files
committed
Add tests
1 parent 5a78ce8 commit 96df6fe

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
.vscode/
22
/test.csv
3-
ascii-data-visualizer
3+
ascii-data-visualizer
4+
5+
# Tests
6+
/tests/test-charts
7+
/tests/test-csv-parser.csv

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ FLAGS = -o ascii-data-visualizer -Iinclude
66
$(TARGET): $(SOURCES)
77
$(CC) $(FLAGS) $(SOURCES)
88

9+
clean:
10+
rm $(TARGET)

tests/Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CC = g++
2+
SOURCES = test_charts.cpp ../src/*.cpp
3+
TARGET = test-charts
4+
FLAGS = -o test-charts -I../include
5+
6+
test-charts: $(SOURCES)
7+
$(CC) $(FLAGS) $(SOURCES)
8+
9+
clean:
10+
rm $(TARGET)

tests/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### Usage Testing
2+
This directory is intended for testing, tests will ensure the program works as intended.
3+
4+
Before testing, navigate into the `tests/` folder:
5+
```bash
6+
$ cd tests/
7+
```
8+
9+
#### Chart Testing
10+
Chart testing will ensure the charts are properly created, e.g. CSV parsing:
11+
```bash
12+
$ make
13+
$ ./test-charts
14+
```
15+
16+
### Cleaning Up
17+
After finishing the tests, you should cleanup the directory:
18+
```bash
19+
$ make clean
20+
```

tests/test_charts.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <fstream>
4+
#include <vector>
5+
#include <cassert>
6+
#include <utility>
7+
#include <filesystem>
8+
#include "chart_manager.h"
9+
#include "file_reader.h"
10+
#include "data_utilities.h"
11+
12+
#define ASSERT(value, errMsg)\
13+
if (!value)\
14+
std::cerr << "[\x1b[31m" << "FAILED\x1b[39m] " << errMsg << "\n";\
15+
assert(value);
16+
17+
void testCsvParser();
18+
bool checkDataPointsMatch(
19+
std::vector<ChartDataPoint>, std::vector<ChartDataPoint>
20+
);
21+
22+
int main()
23+
{
24+
testCsvParser();
25+
std::cout << "all tests passed\n";
26+
return 0;
27+
}
28+
29+
void testCsvParser()
30+
{
31+
if (std::filesystem::exists("test-csv-parser.csv"))
32+
throw std::runtime_error("test file already exists, remove it and try again");
33+
std::string CSV_TITLE = "test";
34+
std::string FILE_NAME = "test-csv-parser.csv";
35+
BarChartInfo myChartInfo;
36+
auto initFile = [CSV_TITLE, FILE_NAME, &myChartInfo](){
37+
std::vector<std::string> lines{
38+
"title: " + CSV_TITLE,
39+
"x,25", "y,12.5", "z,6.25"
40+
};
41+
std::ofstream testFile(FILE_NAME);
42+
for (auto i : lines)
43+
testFile << i + "\n";
44+
testFile.close();
45+
myChartInfo = getBarChartInfoFromFile(FILE_NAME);
46+
};
47+
initFile();
48+
49+
if (std::filesystem::exists("test-csv-parser.csv"))
50+
std::remove("test-csv-parser.csv");
51+
52+
ASSERT((bool)(myChartInfo.title == CSV_TITLE), "failed to parse csv title");
53+
std::vector<ChartDataPoint> validDataPoints{
54+
{ChartDataPoint{.label="x", .value=25}},
55+
{ChartDataPoint{.label="y", .value=12.5}},
56+
{ChartDataPoint{.label="z", .value=6.25}},
57+
};
58+
ASSERT(checkDataPointsMatch(
59+
myChartInfo.dataPoints, validDataPoints),
60+
"error validating chart data points"
61+
);
62+
63+
std::cout << "[ " << "\x1b[32mOK" << "\x1b[39m ] " << "csv parser complete\n";
64+
}
65+
66+
bool checkDataPointsMatch(
67+
std::vector<ChartDataPoint> dataPoint1,
68+
std::vector<ChartDataPoint> dataPoint2)
69+
{
70+
if (dataPoint1.size() != dataPoint2.size())
71+
return false;
72+
int dataPointsCount = dataPoint1.size();
73+
auto checkDataPointsMatch = [](ChartDataPoint firstDp, ChartDataPoint secondDp){
74+
bool labelsMatch = firstDp.label == secondDp.label;
75+
bool valuesMatch = firstDp.value == secondDp.value;
76+
return (labelsMatch && valuesMatch);
77+
};
78+
for (int i = 0; i < dataPointsCount; ++i){
79+
bool isValid = checkDataPointsMatch(
80+
dataPoint1.at(i), dataPoint2.at(i)
81+
);
82+
if (!isValid)
83+
return 0;
84+
}
85+
return 1;
86+
}

0 commit comments

Comments
 (0)