| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
exec 2>&1 |
| 4 |
|
| 5 |
set -e |
| 6 |
|
| 7 |
EXAMPLE_PROGRAM_1=' |
| 8 |
#include <capnp/message.h> |
| 9 |
#include <capnp/serialize-packed.h> |
| 10 |
#include <iostream> |
| 11 |
#include <cstring> |
| 12 |
#include <unistd.h> |
| 13 |
#include <fcntl.h> |
| 14 |
|
| 15 |
#include "person.capnp.h" |
| 16 |
|
| 17 |
static void |
| 18 |
write_person(const char *filename) |
| 19 |
{ |
| 20 |
::capnp::MallocMessageBuilder msg; |
| 21 |
int fd = open(filename, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR); |
| 22 |
KJ_ASSERT(fd >= 0); |
| 23 |
KJ_DEFER(close(fd)); |
| 24 |
|
| 25 |
Person::Builder person = msg.initRoot<Person>(); |
| 26 |
person.setName("Tom Lee"); |
| 27 |
person.setEmail("[email protected]"); |
| 28 |
|
| 29 |
capnp::writePackedMessageToFd(fd, msg); |
| 30 |
} |
| 31 |
|
| 32 |
static void |
| 33 |
read_person(const char *filename) |
| 34 |
{ |
| 35 |
int fd = open(filename, O_RDONLY); |
| 36 |
KJ_ASSERT(fd >= 0); |
| 37 |
KJ_DEFER(close(fd)); |
| 38 |
|
| 39 |
::capnp::PackedFdMessageReader msg(fd); |
| 40 |
|
| 41 |
Person::Reader person = msg.getRoot<Person>(); |
| 42 |
|
| 43 |
std::cout << person.getName().cStr() << "|" << person.getEmail().cStr() << std::endl; |
| 44 |
} |
| 45 |
|
| 46 |
int |
| 47 |
main (int argc, char **argv) |
| 48 |
{ |
| 49 |
KJ_ASSERT(argc == 2); |
| 50 |
write_person(argv[1]); |
| 51 |
read_person(argv[1]); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
' |
| 55 |
|
| 56 |
test_trivial_serde() { |
| 57 |
local ID="$(capnp id)" |
| 58 |
local WORKDIR="$(mktemp -d)" |
| 59 |
local SCHEMAFILE=person.capnp |
| 60 |
local SCHEMA="${WORKDIR}/${SCHEMAFILE}" |
| 61 |
trap "rm -rf '${WORKDIR}'" RETURN EXIT |
| 62 |
|
| 63 |
echo "${ID}; |
| 64 |
|
| 65 |
struct Person { |
| 66 |
name @0 :Text; |
| 67 |
email @1 :Text; |
| 68 |
} |
| 69 |
" >"$SCHEMA" |
| 70 |
( |
| 71 |
cd "$WORKDIR" |
| 72 |
capnp compile -oc++ "$SCHEMAFILE" |
| 73 |
) |
| 74 |
if [ ! -f "$WORKDIR/person.capnp.h" ]; then |
| 75 |
fail "person.capnp.h not found" |
| 76 |
fi |
| 77 |
if [ ! -f "$WORKDIR/person.capnp.c++" ]; then |
| 78 |
fail "person.capnp.c++ not found" |
| 79 |
fi |
| 80 |
|
| 81 |
echo "$EXAMPLE_PROGRAM_1" >"$WORKDIR/trivial-serde-test.cc" |
| 82 |
|
| 83 |
( |
| 84 |
cd "$WORKDIR" |
| 85 |
g++ -Wall -Werror \ |
| 86 |
$(pkg-config --cflags capnp) \ |
| 87 |
-o "trivial-serde-test" \ |
| 88 |
"person.capnp.c++" \ |
| 89 |
"trivial-serde-test.cc" \ |
| 90 |
$(pkg-config --libs capnp) |
| 91 |
) |
| 92 |
|
| 93 |
assertEquals "Tom Lee|[email protected]" "$("$WORKDIR/trivial-serde-test" "$WORKDIR/serialized-person.bin")" |
| 94 |
} |
| 95 |
|
| 96 |
. shunit2 |