GCC Code Coverage Report


Directory: ./
File: test/unittest/src/utils/path_trie.cpp
Date: 2024-07-09 12:21:25
Exec Total Coverage
Lines: 34 34 100.0%
Functions: 13 13 100.0%
Branches: 52 194 26.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2024 Muhammad Nawaz
3 * Licensed under the MIT License. See LICENSE file for more information.
4 */
5 // [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ]
6
7 #include "utils/path_trie.hpp"
8 #include "utils/common.hpp"
9 #include <gtest/gtest.h>
10 #include <string>
11 #include <vector>
12
13 class PathTrieTest: public ::testing::Test
14 {
15 protected:
16 PathTrie trie_;
17
18 // Utility function to simplify the test cases
19 2 bool InsertAndSearch(const std::string& insert_path, const std::string& search_path, std::string& out_oas_path)
20 {
21 2 trie_.Insert(insert_path);
22 2 return trie_.Search(search_path.c_str(), search_path.c_str() + search_path.size(), out_oas_path);
23 }
24 };
25
26 // Test inserting and searching for a simple path
27 8 TEST_F(PathTrieTest, InsertAndSearchSimplePath)
28 {
29 2 std::string oas_path;
30
4/16
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
2 EXPECT_TRUE(InsertAndSearch("/api/data", "/api/data", oas_path));
31
2/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 EXPECT_EQ(oas_path, "/api/data");
32 2 }
33
34 // Test searching for a path that does not exist
35 8 TEST_F(PathTrieTest, SearchNonExistentPath)
36 {
37 2 std::string oas_path;
38
4/16
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
2 EXPECT_FALSE(InsertAndSearch("/api/data", "/api/none", oas_path));
39
1/10
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
2 EXPECT_TRUE(oas_path.empty());
40 2 }
41
42 // Test inserting and searching for a path with a parameter
43 8 TEST_F(PathTrieTest, InsertAndSearchParameterizedPath)
44 {
45 2 std::string oas_path;
46 2 std::unordered_map<size_t, ParamRange> param_idxs;
47
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
2 trie_.Insert("/api/data/{id}");
48
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 std::string search_path = "/api/data/123";
49
2/12
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
2 EXPECT_TRUE(trie_.Search(search_path.data(), search_path.data() + search_path.size(), oas_path, param_idxs));
50
2/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 EXPECT_EQ(oas_path, "/api/data/{id}");
51
3/14
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 26 taken 1 times.
✗ Branch 27 not taken.
2 ASSERT_TRUE(param_idxs.find(3) != param_idxs.end());
52
5/16
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
4 EXPECT_EQ(std::string(param_idxs[3].beg, param_idxs[3].end), "123");
53
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
2 }
54
55 // Test parameterized path with multiple parameters
56 8 TEST_F(PathTrieTest, InsertAndSearchMultiParamPath)
57 {
58 2 std::string oas_path;
59 2 std::unordered_map<size_t, ParamRange> param_idxs;
60
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
2 trie_.Insert("/api/data/{id}/edit/{action}");
61
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 std::string search_path = "/api/data/123/edit/update";
62
2/12
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
2 EXPECT_TRUE(trie_.Search(search_path.data(), search_path.data() + search_path.size(), oas_path, param_idxs));
63
2/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 EXPECT_EQ(oas_path, "/api/data/{id}/edit/{action}");
64
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
2 ASSERT_EQ(param_idxs.size(), 2u);
65
5/16
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
4 EXPECT_EQ(std::string(param_idxs[3].beg, param_idxs[3].end), "123");
66
5/16
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
4 EXPECT_EQ(std::string(param_idxs[5].beg, param_idxs[5].end), "update");
67
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
2 }
68