mirror of
https://github.com/zap-proto/cpp.git
synced 2026-10-30 15:05:47 +00:00
zap-proto/cpp shipped a README telling readers to run cmake, and no CMakeLists, no headers, no source. Every C++ service that needed ZAP therefore hand-wrote frame handling, and the copies drifted from each other and from the wire. So here is the thing itself: the message layer, the call envelope with promise pipelining, and the symmetric transport, behind one exported target zap::zap. The bytes are not asserted against themselves. Every KAT in the codec and rpc suites was printed by github.com/zap-proto/go and pinned here, which is the only kind of evidence that distinguishes agreeing with the wire from agreeing with yourself. That caught one real rule a hand-written port would miss and did: set_bytes must hold its payload until finish(), because the reference lays a list started afterwards down FIRST — same fields, different bytes, and on a chain that is a fork. The transport suite proves the protocol rather than the framing, and without a timer, because a timing-based proof of "in flight at once" is not a proof. Both peers' handlers park until the other's request has arrived, so the test only passes if one connection carried both directions at once. And no request is answered until all eight have arrived; they are then answered in reverse, with the first held back until the caller — which by then already holds the last one's answer — releases it. Streams (direction tags 3-5) are not here. An unknown tag ends the connection rather than being quietly ignored.
83 lines
2.9 KiB
C++
83 lines
2.9 KiB
C++
// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
|
|
// SPDX-License-Identifier: BSD-3-Clause-Eco
|
|
//
|
|
// check.hpp — the whole test harness. A failing check prints where and what,
|
|
// and the process exit code is the verdict.
|
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace zaptest {
|
|
|
|
inline int failures = 0;
|
|
|
|
inline void report(const char* file, int line, const char* what) {
|
|
std::printf("FAIL %s:%d %s\n", file, line, what);
|
|
++failures;
|
|
}
|
|
|
|
inline std::string hex(std::span<const std::uint8_t> b) {
|
|
static const char* d = "0123456789abcdef";
|
|
std::string s;
|
|
s.reserve(b.size() * 2);
|
|
for (std::uint8_t c : b) {
|
|
s.push_back(d[c >> 4]);
|
|
s.push_back(d[c & 0xf]);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
inline std::vector<std::uint8_t> unhex(std::string_view s) {
|
|
auto nib = [](char c) -> int {
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
return -1;
|
|
};
|
|
std::vector<std::uint8_t> out;
|
|
for (std::size_t i = 0; i + 1 < s.size(); i += 2)
|
|
out.push_back(static_cast<std::uint8_t>(nib(s[i]) * 16 + nib(s[i + 1])));
|
|
return out;
|
|
}
|
|
|
|
inline int verdict(const char* suite) {
|
|
if (failures == 0) {
|
|
std::printf("ok %s\n", suite);
|
|
return 0;
|
|
}
|
|
std::printf("FAIL %s: %d check(s)\n", suite, failures);
|
|
return 1;
|
|
}
|
|
|
|
} // namespace zaptest
|
|
|
|
#define CHECK(cond) \
|
|
do { \
|
|
if (!(cond)) zaptest::report(__FILE__, __LINE__, #cond); \
|
|
} while (0)
|
|
|
|
#define CHECK_EQ(got, want) \
|
|
do { \
|
|
auto _g = (got); \
|
|
auto _w = (want); \
|
|
if (!(_g == _w)) \
|
|
zaptest::report(__FILE__, __LINE__, #got " != " #want); \
|
|
} while (0)
|
|
|
|
#define CHECK_HEX(got, want) \
|
|
do { \
|
|
const std::string _g = zaptest::hex(got); \
|
|
const std::string _w = (want); \
|
|
if (_g != _w) { \
|
|
std::printf(" got %s\n want %s\n", _g.c_str(), _w.c_str()); \
|
|
zaptest::report(__FILE__, __LINE__, "bytes differ"); \
|
|
} \
|
|
} while (0)
|