Fixed tests in the packet module
This commit is contained in:
parent
43b3ca386d
commit
ee3cba3cab
1 changed files with 54 additions and 27 deletions
|
|
@ -30,14 +30,14 @@ const ContextFlag = enum(u1) { // Meaning depends on packet context
|
|||
};
|
||||
const PropagationType = enum(u1) { broadcast = 0, transport = 1 };
|
||||
|
||||
const DestinationType = enum(u2) { //
|
||||
const DestinationType = enum(u2) {
|
||||
single = 0b0,
|
||||
group = 0b01,
|
||||
plain = 0b10,
|
||||
link = 0b11,
|
||||
};
|
||||
|
||||
const PacketType = enum(u2) { //
|
||||
const PacketType = enum(u2) {
|
||||
data = 0b0,
|
||||
announce = 0b01,
|
||||
link_request = 0b10,
|
||||
|
|
@ -78,7 +78,7 @@ const PacketHeader = packed struct {
|
|||
propagation: PropagationType,
|
||||
destination: DestinationType,
|
||||
packet: PacketType,
|
||||
hops: u8, //
|
||||
hops: u8,
|
||||
};
|
||||
|
||||
const Packet = struct {
|
||||
|
|
@ -86,7 +86,7 @@ const Packet = struct {
|
|||
address1: [ADDRESS_SIZE]u8,
|
||||
address2: [ADDRESS_SIZE]u8,
|
||||
context: PacketContext,
|
||||
data: []const u8, //
|
||||
data: []const u8,
|
||||
};
|
||||
|
||||
///////////////// Functions
|
||||
|
|
@ -133,10 +133,12 @@ pub fn serializePacket(packet: Packet, output_buffer: []u8) !usize {
|
|||
}
|
||||
|
||||
fn copyToPacket(dst: anytype, src: []const u8, offset: usize, count: usize) !usize {
|
||||
if (src.len > offset + count) {
|
||||
if (src.len < offset + count) {
|
||||
return error.BufferTooShort;
|
||||
}
|
||||
@memcpy(@as(*u8, &dst), src[offset .. offset + count]);
|
||||
|
||||
const dst_ptr: []u8 = @ptrCast(dst);
|
||||
@memcpy(dst_ptr, src[offset .. offset + count]);
|
||||
|
||||
return offset + count;
|
||||
}
|
||||
|
|
@ -160,6 +162,9 @@ pub fn deserializePacket(message_buffer: []u8, dest_packet: *Packet) !void {
|
|||
///////////////// Tests
|
||||
//
|
||||
|
||||
//////// Helper functions
|
||||
//
|
||||
|
||||
// Serializes the given packet and makes sure the output is correct
|
||||
fn testPacketSerialization(packet: Packet) !void {
|
||||
const buf_size = comptime @sizeOf(Packet) + MAX_DATA_SIZE;
|
||||
|
|
@ -188,6 +193,28 @@ fn testPacketSerialization(packet: Packet) !void {
|
|||
offset += packet.data.len;
|
||||
}
|
||||
|
||||
fn headersEql(h1: *const PacketHeader, h2: *const PacketHeader) bool {
|
||||
return h1.ifac == h2.ifac
|
||||
and h1.header == h2.header
|
||||
and h1.context == h2.context
|
||||
and h1.propagation == h2.propagation
|
||||
and h1.destination == h2.destination
|
||||
and h1.packet == h2.packet
|
||||
and h1.hops == h2.hops;
|
||||
}
|
||||
|
||||
fn packetsEql(p1: *const Packet, p2: *const Packet) bool {
|
||||
return headersEql(&p1.header, &p2.header)
|
||||
and memeql(u8, &p1.address1, &p2.address1)
|
||||
and memeql(u8, &p1.address2, &p2.address2)
|
||||
and p1.context == p2.context
|
||||
and p1.data.len == p2.data.len
|
||||
and memeql(u8, p1.data, p2.data);
|
||||
}
|
||||
|
||||
//////// Tests
|
||||
//
|
||||
|
||||
test "Structs size" {
|
||||
try expect(@sizeOf(PacketHeader) == 2);
|
||||
}
|
||||
|
|
@ -320,5 +347,5 @@ test "Serialize / Deserialize Packet: Header type2, Medium data size" {
|
|||
_ = try serializePacket(packet, &buf);
|
||||
try deserializePacket(&buf, &res_packet);
|
||||
|
||||
try expect(res_packet == packet);
|
||||
try expect(packetsEql(&res_packet, &packet));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue