|
| 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