Skip to content

Commit ba0f995

Browse files
committed
Add more unit tests for higher test coverage
1 parent d6da8f3 commit ba0f995

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

tests/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ find_package(GTest REQUIRED)
66

77
add_executable(
88
msgpack23_tests
9+
array_tests.cpp
910
int8_tests.cpp
1011
int16_tests.cpp
1112
int32_tests.cpp
1213
int64_tests.cpp
1314
main.cpp
15+
map_tests.cpp
1416
object_packing_tests.cpp
17+
string_tests.cpp
18+
type_packing_tests.cpp
1519
uint8_tests.cpp
1620
uint16_tests.cpp
1721
uint32_tests.cpp
1822
uint64_tests.cpp
19-
type_packing_tests.cpp
2023
)
2124

2225
target_link_libraries(

tests/array_tests.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Created by Rene Windegger on 16/02/2025.
3+
//
4+
5+
#include <gtest/gtest.h>
6+
#include <msgpack23/msgpack23.h>
7+
8+
namespace {
9+
class msgpack23_array : public testing::TestWithParam<std::size_t> {
10+
};
11+
12+
TEST_P(msgpack23_array, arrayTest) {
13+
std::vector<std::size_t> expected {};
14+
for (std::size_t i = 0; i < GetParam(); ++i) {
15+
expected.emplace_back(i);
16+
}
17+
msgpack23::Packer packer {};
18+
auto data = packer(expected);
19+
msgpack23::Unpacker unpacker {data.data(), data.size()};
20+
std::vector<std::size_t> actual {};
21+
unpacker(actual);
22+
EXPECT_EQ(actual, expected);
23+
}
24+
25+
TEST_P(msgpack23_array, binaryTest) {
26+
std::vector<std::uint8_t> expected {};
27+
for (std::size_t i = 0; i < GetParam(); ++i) {
28+
expected.emplace_back(42U);
29+
}
30+
msgpack23::Packer packer {};
31+
auto data = packer(expected);
32+
msgpack23::Unpacker unpacker {data.data(), data.size()};
33+
std::vector<std::uint8_t> actual {};
34+
unpacker(actual);
35+
EXPECT_EQ(actual, expected);
36+
}
37+
38+
constexpr std::size_t array_sizes[] = {
39+
1,
40+
std::numeric_limits<int8_t>::max(),
41+
42,
42+
std::numeric_limits<uint16_t>::max() - 1,
43+
std::numeric_limits<uint16_t>::max() + 1,
44+
};
45+
INSTANTIATE_TEST_SUITE_P(SomeValuesTest, msgpack23_array, testing::ValuesIn(array_sizes));
46+
}

tests/map_tests.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Created by Rene Windegger on 16/02/2025.
3+
//
4+
5+
#include <gtest/gtest.h>
6+
#include <map>
7+
#include <msgpack23/msgpack23.h>
8+
9+
namespace {
10+
class msgpack23_map : public testing::TestWithParam<std::size_t> {
11+
};
12+
13+
TEST_P(msgpack23_map, arrayTest) {
14+
std::map<std::size_t, std::size_t> expected {};
15+
for (std::size_t i = 0; i < GetParam(); ++i) {
16+
expected.insert_or_assign(i, i);
17+
}
18+
msgpack23::Packer packer {};
19+
auto data = packer(expected);
20+
msgpack23::Unpacker unpacker {data.data(), data.size()};
21+
std::map<std::size_t, std::size_t> actual {};
22+
unpacker(actual);
23+
EXPECT_EQ(actual, expected);
24+
}
25+
26+
constexpr std::size_t map_sizes[] = {
27+
1,
28+
std::numeric_limits<int8_t>::max(),
29+
42,
30+
std::numeric_limits<uint16_t>::max() - 1,
31+
std::numeric_limits<uint16_t>::max() + 1,
32+
};
33+
INSTANTIATE_TEST_SUITE_P(SomeValuesTest, msgpack23_map, testing::ValuesIn(map_sizes));
34+
}

tests/string_tests.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Created by Rene Windegger on 16/02/2025.
3+
//
4+
5+
#include <gtest/gtest.h>
6+
#include <map>
7+
#include <msgpack23/msgpack23.h>
8+
9+
namespace {
10+
class msgpack23_string : public testing::TestWithParam<std::size_t> {
11+
};
12+
13+
TEST_P(msgpack23_string, arrayTest) {
14+
std::string expected {};
15+
for (std::size_t i = 0; i < GetParam(); ++i) {
16+
expected.append("*");
17+
}
18+
msgpack23::Packer packer {};
19+
auto data = packer(expected);
20+
msgpack23::Unpacker unpacker {data.data(), data.size()};
21+
std::string actual {};
22+
unpacker(actual);
23+
EXPECT_EQ(actual, expected);
24+
}
25+
26+
constexpr std::size_t string_sizes[] = {
27+
1,
28+
std::numeric_limits<int8_t>::max(),
29+
42,
30+
std::numeric_limits<uint16_t>::max() - 1,
31+
std::numeric_limits<uint16_t>::max() + 1,
32+
};
33+
INSTANTIATE_TEST_SUITE_P(SomeValuesTest, msgpack23_string, testing::ValuesIn(string_sizes));
34+
}

0 commit comments

Comments
 (0)