A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
test-adjacency-matrix.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7 */
8
9#include "ns3/symmetric-adjacency-matrix.h"
10#include "ns3/test.h"
11
12using namespace ns3;
13
14/**
15 * @ingroup antenna-tests
16 *
17 * @brief SymmetricAdjacencyMatrix Test Case
18 */
20{
21 public:
22 /**
23 * The constructor of the test case
24 */
26 : TestCase("SymmetricAdjacencyMatrix test case")
27 {
28 }
29
30 private:
31 /**
32 * Run the test
33 */
34 void DoRun() override;
35};
36
37void
39{
42 0,
43 "Should have 0 rows, but have " << boolAdj.GetRows());
44 boolAdj.AddRow();
46 1,
47 "Should have 1 rows, but have " << boolAdj.GetRows());
48 boolAdj.AddRow();
50 2,
51 "Should have 2 rows, but have " << boolAdj.GetRows());
52 boolAdj.AddRow();
54 3,
55 "Should have 3 rows, but have " << boolAdj.GetRows());
56 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(0, 0), false, "Should be set to false");
57 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 0), false, "Should be set to false");
58 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 1), false, "Should be set to false");
59 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 0), false, "Should be set to false");
60 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 1), false, "Should be set to false");
61 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 2), false, "Should be set to false");
62
63 // Test constructor with arguments
64 boolAdj = SymmetricAdjacencyMatrix<bool>(3, true);
65 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(0, 0), true, "Should be set to false");
66 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 0), true, "Should be set to false");
67 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 1), true, "Should be set to false");
68 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 0), true, "Should be set to false");
69 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 1), true, "Should be set to false");
70 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 2), true, "Should be set to false");
71
72 // Set value setting
73 boolAdj = SymmetricAdjacencyMatrix<bool>(4, false);
75 4,
76 "Should have 4 rows, but have " << boolAdj.GetRows());
77 for (int i = 0; i < 4; i++)
78 {
79 // Mark all adjacent values to row i to true
80 boolAdj.SetValueAdjacent(i, true);
81 for (int j = 0; j < 4; j++)
82 {
83 for (int k = 0; k < 4; k++)
84 {
85 // Check if adjacent values to i were marked as true
86 if (i == j || i == k)
87 {
88 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(j, k), true, "Should be set to true");
89 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(k, j), true, "Should be set to true");
90 }
91 else
92 {
93 // Check if all other values are marked as false
94 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(j, k), false, "Should be set to false");
95 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(k, j), false, "Should be set to false");
96 }
97 }
98 }
99 // Reset values
100 for (int j = 0; j < 4; j++)
101 {
102 for (int k = 0; k < 4; k++)
103 {
104 if (i == j || i == k)
105 {
106 boolAdj.SetValue(j, k, false);
107 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(j, k), false, "Should be set to false");
108 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(k, j), false, "Should be set to false");
109 }
110 }
111 }
112 }
113}
114
115/**
116 * @ingroup core-tests
117 *
118 * @brief AdjacencyMatrix Test Suite
119 */
121{
122 public:
124};
125
127 : TestSuite("adjacency-matrix-test", Type::UNIT)
128{
129 AddTestCase(new SymmetricAdjacencyMatrixTestCase(), TestCase::Duration::QUICK);
130}
131
AdjacencyMatrix Test Suite.
SymmetricAdjacencyMatrix Test Case.
SymmetricAdjacencyMatrixTestCase()
The constructor of the test case.
void DoRun() override
Run the test.
A class representing a symmetric adjacency matrix.
T GetValue(size_t row, size_t column)
Retrieve the value of matrix (row, column) node.
void SetValueAdjacent(size_t row, T value)
Set the value of adjacent nodes of a given node (all columns of a given row, and its reflection)
size_t GetRows()
Retrieve number of rows in the adjacency matrix.
void AddRow()
Add new row to the adjacency matrix.
void SetValue(size_t row, size_t column, T value)
Set the value of matrix (row, column) node.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static AdjacencyMatrixTestSuite adjacencyMatrixTestSuiteInstance