This commit is contained in:
2026-04-28 09:42:27 -05:00
parent 8ff66ac1e2
commit 44a51acad1
6 changed files with 107 additions and 32 deletions

View File

@@ -10,6 +10,18 @@
using namespace tyche;
static void print(std::vector<uint8_t> const& data)
{
for (size_t i = 0; i < data.size(); ++i) {
if (i % 16 == 0)
printf("%04X : ", i);
printf("%02X ", data.at(i));
if (i % 16 == 15)
printf("\n");
}
printf("\n");
}
TEST(ByteArray, ByteArray)
{
auto test = [](std::function<void(ByteArray&)> const& f, std::vector<uint8_t> const& expected) {
@@ -55,14 +67,14 @@ TEST(Bytecode, Constants)
0x00, 0x00, 0x00, 0x00,
// index
0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x02, 0x00, // constants
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // functions
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// constant indexes
0x00, 0x00, 0x00, 0x00,
@@ -94,14 +106,14 @@ TEST(Bytecode, Code)
0x00, 0x00, 0x00, 0x00,
// index
0x00, 0x00, 0x00, 0x00,
0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // constants
0x40, 0x00, 0x00, 0x00, 0x02, 0x00, // variables
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// function definitions
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -112,6 +124,34 @@ TEST(Bytecode, Code)
};
}
TEST(Bytecode, Parsing)
{
// write bytecode
BytecodePrototype bp;
bp.constants.emplace_back(42);
bp.constants.emplace_back(3.14f);
bp.constants.emplace_back("HELLO");
auto& f = bp.functions.emplace_back(0, 0);
f.code.append_byte(0x68);
f.code.append_int(42);
auto& f2 = bp.functions.emplace_back(2, 1);
f2.code.append_byte(0x42);
Bytecode bc1(bp);
auto data = bc1.data();
// read bytecode
Bytecode bc2(data);
ASSERT_EQ(bc2.n_constants(), 3);
ASSERT_EQ(bc2.n_functions(), 2);
}
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);