Skip to content

Commit d6b997f

Browse files
Implemented FFI testing, mocked out bad cases and verified the correct operation of Rust modules
1 parent c8ac1d5 commit d6b997f

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

tests-ffi/time.cxx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <cassert>
2+
#include <cstdint>
3+
#include <string>
4+
5+
using std::string;
6+
using std::uint8_t;
7+
8+
extern "C" enum class AllocResult : uint8_t {
9+
OK,
10+
NULL_DEFERENCE_ERR,
11+
CONVERT_TO_STR_FAILED,
12+
};
13+
14+
extern "C" AllocResult time_now(char **out);
15+
extern "C" void raw_free_c_str(char *str);
16+
17+
void null_deference_test() {
18+
using R = AllocResult;
19+
20+
auto result = time_now(nullptr);
21+
assert(result != R::CONVERT_TO_STR_FAILED);
22+
assert(result != R::OK);
23+
assert(result == R::NULL_DEFERENCE_ERR);
24+
}
25+
26+
void time_now_test() {
27+
using R = AllocResult;
28+
29+
char *out = nullptr;
30+
auto result = time_now(&out);
31+
auto time_now_string = string(out);
32+
raw_free_c_str(out);
33+
34+
assert(result != R::CONVERT_TO_STR_FAILED);
35+
assert(result != R::NULL_DEFERENCE_ERR);
36+
assert(result == R::OK);
37+
assert(!time_now_string.empty());
38+
}
39+
40+
int main() {
41+
null_deference_test();
42+
time_now_test();
43+
return 0;
44+
}

0 commit comments

Comments
 (0)