This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_oid.cpp
More file actions
100 lines (81 loc) · 3.23 KB
/
Copy pathtest_oid.cpp
File metadata and controls
100 lines (81 loc) · 3.23 KB
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
#include <cppgit2/oid.hpp>
#include <doctest.hpp>
using doctest::test_suite;
using namespace cppgit2;
TEST_CASE("Construct oid from string" * test_suite("oid")) {
oid oid("f9de917ac729414151fdce077d4098cfec9a45a5");
REQUIRE(oid.to_hex_string() == "f9de917ac729414151fdce077d4098cfec9a45a5");
}
TEST_CASE("Construct oid from C ptr" * test_suite("oid")) {
oid oid1("f9de917ac729414151fdce077d4098cfec9a45a5");
oid oid2(oid1.c_ptr());
REQUIRE(oid2.to_hex_string() == "f9de917ac729414151fdce077d4098cfec9a45a5");
}
TEST_CASE("Invalid oid construction" * test_suite("oid")) {
bool exception_thrown = false;
try {
oid foo("f9de90000&^$%_%#");
} catch (git_exception &) {
exception_thrown = true;
}
REQUIRE(exception_thrown);
}
TEST_CASE("Clone oid from existing oid struct" * test_suite("oid")) {
oid id("f9de917ac729414151fdce077d4098cfec9a45a5");
oid clone = id.copy();
REQUIRE(clone.to_hex_string() == "f9de917ac729414151fdce077d4098cfec9a45a5");
}
TEST_CASE("Get path formatted string from oid" * test_suite("oid")) {
oid id("f9de917ac729414151fdce077d4098cfec9a45a5");
REQUIRE(id.to_path_string() == "f9/de917ac729414151fdce077d4098cfec9a45a5");
}
TEST_CASE("Construct oid from string with 5 characters" * test_suite("oid")) {
oid oid("f9de917ac729414151fdce077d4098cfec9a45a5", 5);
REQUIRE(oid.to_hex_string(5) == "f9de9");
}
TEST_CASE("to_hex_string() on oid constructed with 5 characters has 35 zeros" *
test_suite("oid")) {
oid oid("f9de917ac729414151fdce077d4098cfec9a45a5", 5);
REQUIRE(oid.to_hex_string() == "f9de900000000000000000000000000000000000");
}
TEST_CASE("Check if oid is all zeros" * test_suite("oid")) {
oid oid1("f9de917ac729414151fdce077d4098cfec9a45a5");
REQUIRE(oid1.is_zero() == false);
oid oid2("f9de917ac729414151fdce077d4098cfec9a45a5", 5);
REQUIRE(oid2.is_zero() == false);
oid oid3("00000", 3);
REQUIRE(oid3.is_zero() == true);
oid oid4("0000000000000000000000000000000000000000");
REQUIRE(oid4.is_zero() == true);
}
TEST_CASE("Check if two oids are equal" * test_suite("oid")) {
oid oid1("f9de917ac729414151fdce077d4098cfec9a45a5");
oid oid2("f9de917ac729414151fdce077d4098cfec9a45a5");
REQUIRE_FALSE(oid1 == "fa64cv");
REQUIRE(oid1 == "f9de917ac729414151fdce077d4098cfec9a45a5");
REQUIRE(oid1 == oid2);
REQUIRE(oid2 == oid1);
}
TEST_CASE("Compare two oids" * test_suite("oid")) {
oid oid1("f9de917ac729414151fdce077d4098cfec9a45a5");
oid oid2("698b74a011ce48d2cae918129e8392c8987e0777");
REQUIRE(oid1.compare(oid2) > 0);
REQUIRE(oid2.compare(oid1) < 0);
oid oid3("69de917ac729414151fdce077d4098cfec9a45a5");
REQUIRE(oid2.compare(oid3) < 0);
REQUIRE(oid2.compare(oid3, 2) == 0);
oid oid4("f9de900000000000000000000000000000000000");
REQUIRE(oid1.compare(oid4, 5) == 0);
}
TEST_CASE("Interoperatbility with libgit2" * test_suite("oid")) {
// Construct cppgit2 OID object
oid oid1("f9de917ac729414151fdce077d4098cfec9a45a5");
// Access libgit2 C ptr
auto oid1_cptr = oid1.c_ptr();
// Use the libgit2 C API to format
size_t n = 8;
char *oid1_formatted = (char *)malloc(sizeof(char) * n);
git_oid_tostr(oid1_formatted, n + 1, oid1_cptr);
// Results are the same
REQUIRE(oid1.to_hex_string(8) == std::string(oid1_formatted)); // f9de917
}