1- #include < stdio.h>
2- extern " C" int remote_function (int (*callback)(int ), int value);
1+ #include < cassert>
2+ #include < cstdio>
3+ #include < array>
4+ #include < memory_resource>
5+ #include " json.hpp"
6+ // Test 1: Simple remote function
7+ extern int remote_function (int (*callback)(int ), int value);
8+ // Test 2: Remote allocation with polymorphic memory resource
9+ static std::vector<std::byte> buffer (65536 );
10+ static std::pmr::monotonic_buffer_resource mbr{buffer.data (), buffer.size ()};
11+ extern std::pmr::vector<int > remote_allocation (std::pmr::memory_resource* mr, size_t size);
12+ // Test 3: RapidJSON using same polymorphic memory resource
13+ extern void remote_json (JsonDocument& j);
14+ #define my_assert (x ) do { if (!(x)) { printf (" Assertion failed: %s\n " , #x); std::abort (); } } while (0 )
315
416static int double_int (int value)
517{
@@ -9,11 +21,61 @@ static int double_int(int value)
921int main ()
1022{
1123 printf (" Hello from Main VM!\n " );
12- fflush (stdout);
13- for (int i = 0 ; i < 10 ; i++) {
14- const int val = remote_function (double_int, 21 );
15- printf (" Returned value: %d\n " , val);
24+ if constexpr (true ) {
25+ for (int i = 0 ; i < 10 ; i++) {
26+ const int val = remote_function (double_int, 21 );
27+ my_assert (val == 42 );
28+ }
29+ printf (" * Verified remote_function works\n " );
30+ }
31+ if constexpr (true ) {
32+ std::pmr::memory_resource* mr = &mbr;
33+ for (int i = 0 ; i < 10 ; i++) {
34+ std::pmr::vector<int > vec = remote_allocation (mr, 1024 );
35+ my_assert (!vec.empty ());
36+ my_assert (vec.size () == 1024 );
37+ for (size_t j = 0 ; j < vec.size (); j++) {
38+ my_assert (vec[j] == j);
39+ // printf("%d ", vec[j]);
40+ }
41+ }
42+ printf (" * Verified remote_allocation works\n " );
1643 }
44+ if constexpr (true ) {
45+ char valueBuffer[8192 ];
46+ char parseBuffer[2048 ];
47+ rapidjson::MemoryPoolAllocator<> valueAllocator (valueBuffer, sizeof (valueBuffer));
48+ rapidjson::MemoryPoolAllocator<> parseAllocator (parseBuffer, sizeof (parseBuffer));
49+ JsonDocument j (&valueAllocator, sizeof (parseBuffer), &parseAllocator);
50+ remote_json (j);
51+ my_assert (!j.HasParseError ());
52+ my_assert (j.IsObject ());
53+ my_assert (j.HasMember (" key" ));
54+ my_assert (j[" key" ].IsString ());
55+ my_assert (strcmp (j[" key" ].GetString (), " value" ) == 0 );
56+ my_assert (j.HasMember (" number" ));
57+ my_assert (j[" number" ].IsInt ());
58+ my_assert (j[" number" ].GetInt () == 42 );
59+ my_assert (j.HasMember (" array" ));
60+ my_assert (j[" array" ].IsArray ());
61+ const auto & arr = j[" array" ];
62+ my_assert (arr.Size () == 3 );
63+ my_assert (arr[0 ].IsInt () && arr[0 ].GetInt () == 1 );
64+ my_assert (arr[1 ].IsInt () && arr[1 ].GetInt () == 2 );
65+ my_assert (arr[2 ].IsInt () && arr[2 ].GetInt () == 3 );
66+ my_assert (j.HasMember (" boolean" ));
67+ my_assert (j[" boolean" ].IsBool ());
68+ my_assert (j[" boolean" ].GetBool () == true );
69+ // Pretty print the Document
70+ rapidjson::StringBuffer buffer;
71+ rapidjson::PrettyWriter<rapidjson::StringBuffer> writer (buffer);
72+ writer.SetIndent (' ' , 4 ); // Use 4 spaces for indentation
73+ j.Accept (writer);
74+ printf (" Remote JSON document: %s\n " , buffer.GetString ());
75+
76+ printf (" * Verified remote_json works\n " );
77+ }
78+ fflush (stdout);
1779 return 0 ;
1880}
1981
0 commit comments