-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
113 lines (89 loc) · 3.46 KB
/
test.cpp
File metadata and controls
113 lines (89 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <iomanip>
#include <string>
#include <filesystem>
#include <cassert>
#include "include/stampdb.hpp"
using namespace std;
namespace fs = std::filesystem;
int main() {
try {
// Clean up any existing test files
fs::remove("test_db.csv");
fs::remove("test_db.csv.tmp");
// Create a test CSV file
{
ofstream testFile("test_db.csv");
testFile << "time, id, name, value, active\n";
testFile << "1.0, 1001, Item 1, 10.5, true\n";
testFile << "2.0, 1002, Item 2, 20.5, false\n";
testFile << "3.0, 1003, Item 3, 30.5, true\n";
}
cout << "=== Testing StampDB ===\n";
// Test 1: Initialize database
cout << "\n[Test 1] Initializing database...\n";
StampDB db("test_db.csv");
cout << "Database initialized successfully.\n";
// Test 2: Read existing data
cout << "\n[Test 2] Reading existing data...\n";
auto allData = db.read_range(0, 10);
cout << "Found " << allData.points.size() << " data points.\n";
printCSVData(allData);
// Test 3: Read single point
cout << "[Test 3] Reading single point (time=2.0)...\n";
auto point = db.read(2.0);
if (!point.points.empty()) {
const PointRow& row = point.points.at(0).rows.at(0);
cout << "Found point at time 2.0:\n";
std::cout << "Row of the Point: ";
std::visit([](const auto& value){
std::cout << value;
}, row.data);
std::cout << std::endl;
} else {
cout << "No point found at time 2.0\n";
}
// Test 4: Add new point
cout << "\n[Test 4] Adding new point...\n";
Point newPoint;
newPoint.time = 4.0;
// Add each field as a separate PointRow
newPoint.rows.push_back({1004}); // id
newPoint.rows.push_back({"Item 4"}); // name
newPoint.rows.push_back({40.5}); // value
newPoint.rows.push_back({true}); // active
db.appendPoint(newPoint);
cout << "\n New Point Appended. Now starting compaction." << endl;
db.compact();
cout << "New point added. Current data:\n";
printCSVData(db.read_range(1, 4));
// Test 5: Delete point
cout << "\n[Test 5] Deleting point at time=2.0...\n";
db.delete_point(2.0);
db.compact();
cout << "After deletion. Current data:\n";
printCSVData(db.read_range(0, 10));
// Test 6: Force a checkpoint
cout << "\n[Test 6] Forcing checkpoint...\n";
db.compact();
cout << "Checkpoint completed.\n";
// Test 7: Test persistence by creating a new instance
cout << "\n[Test 7] Testing persistence...\n";
{
StampDB db2("test_db.csv");
cout << "Data after reloading from disk:\n";
printCSVData(db2.read_range(0, 10));
}
// Test 8: Compact the database
cout << "\n[Test 8] Compacting database...\n";
db.compact();
cout << "Compaction completed.\n";
// Clean up
db.close();
cout << "\n=== All tests completed successfully! ===\n";
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}