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 private:
29 /**
30 * Run the test
31 */
32 void DoRun() override;
33};
34
35void
37{
40 0,
41 "Should have 0 rows, but have " << boolAdj.GetRows());
42 boolAdj.AddRow();
44 1,
45 "Should have 1 rows, but have " << boolAdj.GetRows());
46 boolAdj.AddRow();
48 2,
49 "Should have 2 rows, but have " << boolAdj.GetRows());
50 boolAdj.AddRow();
52 3,
53 "Should have 3 rows, but have " << boolAdj.GetRows());
54 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(0, 0), false, "Should be set to false");
55 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 0), false, "Should be set to false");
56 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 1), false, "Should be set to false");
57 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 0), false, "Should be set to false");
58 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 1), false, "Should be set to false");
59 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 2), false, "Should be set to false");
60
61 // Test constructor with arguments
62 boolAdj = SymmetricAdjacencyMatrix<bool>(3, true);
63 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(0, 0), true, "Should be set to false");
64 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 0), true, "Should be set to false");
65 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(1, 1), true, "Should be set to false");
66 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 0), true, "Should be set to false");
67 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 1), true, "Should be set to false");
68 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(2, 2), true, "Should be set to false");
69
70 // Set value setting
71 boolAdj = SymmetricAdjacencyMatrix<bool>(4, false);
73 4,
74 "Should have 4 rows, but have " << boolAdj.GetRows());
75 for (int i = 0; i < 4; i++)
76 {
77 // Mark all adjacent values to row i to true
78 boolAdj.SetValueAdjacent(i, true);
79 for (int j = 0; j < 4; j++)
80 {
81 for (int k = 0; k < 4; k++)
82 {
83 // Check if adjacent values to i were marked as true
84 if (i == j || i == k)
85 {
86 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(j, k), true, "Should be set to true");
87 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(k, j), true, "Should be set to true");
88 }
89 else
90 {
91 // Check if all other values are marked as false
92 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(j, k), false, "Should be set to false");
93 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(k, j), false, "Should be set to false");
94 }
95 }
96 }
97 // Reset values
98 for (int j = 0; j < 4; j++)
99 {
100 for (int k = 0; k < 4; k++)
101 {
102 if (i == j || i == k)
103 {
104 boolAdj.SetValue(j, k, false);
105 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(j, k), false, "Should be set to false");
106 NS_TEST_EXPECT_MSG_EQ(boolAdj.GetValue(k, j), false, "Should be set to false");
107 }
108 }
109 }
110 }
111}
112
113/**
114 * @ingroup core-tests
115 *
116 * @brief AdjacencyMatrix Test Suite
117 */
119{
120 public:
122};
123
125 : TestSuite("adjacency-matrix-test", Type::UNIT)
126{
127 AddTestCase(new SymmetricAdjacencyMatrixTestCase(), TestCase::Duration::QUICK);
128}
129
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