-
Notifications
You must be signed in to change notification settings - Fork 5
/
gtest_hw2_nodes.cpp
143 lines (110 loc) · 3.91 KB
/
gtest_hw2_nodes.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <gtest/gtest.h>
#include <fstream>
#include "CME212/Util.hpp"
#include "CME212/Point.hpp"
#include "Graph.hpp"
class GraphPointFixture : public ::testing::Test {
protected:
//Define types
using GraphType = Graph<int,int>;
using NodeType = typename GraphType::node_type;
using EdgeType = typename GraphType::edge_type;
//Set up Graph and Points
GraphType graph;
std::vector<Point> points;
Point p;
virtual void SetUp() {
p = Point(CME212::random(), CME212::random(), CME212::random());
}
};
class SingleNodeFixture : public ::testing::Test {
protected:
//Define types
using GraphType = Graph<int,int>;
using NodeType = typename GraphType::node_type;
//Set up Graph and Node
GraphType graph;
Point p;
NodeType node;
virtual void SetUp() {
p = Point(CME212::random(), CME212::random(), CME212::random());
graph.add_node(p);
node = graph.node(0);
}
};
class TwoGraphFixture : public ::testing::Test {
protected:
//Define types
using GraphType = Graph<int,int>;
using NodeType = typename GraphType::node_type;
//Set up Graph and Nodes
GraphType g, g2;
};
// test Node Size
TEST_F(GraphPointFixture, NodeSize){
EXPECT_LE( sizeof(NodeType), 16 ) << "node size > 16 bytes";
}
// Test add node
TEST_F(GraphPointFixture, AddNode){
graph.add_node(p);
EXPECT_EQ(graph.num_nodes(),1) << "Graph does not have 1 Node";
NodeType node = graph.node(0);
EXPECT_EQ(node.position() , p) << "Node position not conserved";
EXPECT_EQ(node.index(), 0) << "Index is not 0";
EXPECT_EQ(node.value(), 0) << "Node value is not default";
}
// Test setting node value
TEST_F(SingleNodeFixture, SetNodeValue){
graph.node(0).value() = 2;
EXPECT_EQ(graph.node(0).value(),2) << "New Node value from graph is incorrect";
EXPECT_EQ(node.value(),2) << "New Node value from proxy node is incorrect";
}
// Test node removal
TEST_F(SingleNodeFixture, RemoveNode){
EXPECT_TRUE(graph.remove_node(node)) << "Node removal returned false";
EXPECT_EQ(graph.num_nodes(),0) << "Graph noes not have 0 Nodes after removal";
}
// Test mltiple node removal
TEST_F(GraphPointFixture, RepeatedRemoveNode){
//add nodes
for (int k = 0; k < 100; ++k)
graph.add_node(Point(CME212::random(), CME212::random(), CME212::random()), k);
//check succesful add_node
EXPECT_EQ(graph.node(0).value(),0) << "Node value set error node (0)";
EXPECT_EQ(graph.node(53).value(),53) << "Node value set error (53)";
EXPECT_EQ(graph.node(99).value(),99) << "Node value set error (99)";
//check removeal
for (unsigned k = 0; k < 50; ++k) {
unsigned n = unsigned(CME212::random(0, graph.num_nodes()));
graph.remove_node(graph.node(n));
}
EXPECT_EQ( graph.num_nodes(), 50 ) << " Did not remove 50 nodes";
//check node indices
bool succ = true;
for (unsigned k = 0; succ && k < 50; ++k) {
NodeType node = graph.node(k);
if (node.index() != k)
succ = false;
}
EXPECT_TRUE(succ);
//test clearing
graph.clear();
EXPECT_EQ(graph.num_nodes(), 0) << "Graph does not have 0 Nodes after removal";
}
// Test comparisons
TEST_F(TwoGraphFixture, CompareNodes){
//Adding 50 Nodes to Graph1 and Graph2
for (unsigned k = 0; k < 50; ++k) {
Point p(CME212::random(), CME212::random(), CME212::random());
g.add_node(p);
g2.add_node(p);
}
EXPECT_EQ(g.num_nodes(), 50) << "g does not have 50 nodes";
EXPECT_EQ(g2.num_nodes(), 50) << "g2 does not have 50 nodes";
EXPECT_EQ(g2.node(23), g2.node(23)) << "G2-G2 Node comparison == error";
EXPECT_NE(g2.node(23), g2.node(21)) << "G2-G2 Node comparison != error";
EXPECT_NE(g2.node(23), g.node(23)) << "G2-G1 Node comparison != error";
EXPECT_NE(g2.node(23), g.node(21)) << "G2-G1 Node comparison != error";
EXPECT_TRUE(g.node(23) < g.node(21) || g.node(23) > g.node(21)) << "G1-G1 Node comparison < > error";
EXPECT_TRUE(g.node(23) < g2.node(21) || g.node(23) > g2.node(21)) << "G1-G2 Node comparison < > error";
}