diff --git a/src/space_packet/primary_hdr.gleam b/src/space_packet/primary_hdr.gleam index 2e65b04..ce40e3d 100644 --- a/src/space_packet/primary_hdr.gleam +++ b/src/space_packet/primary_hdr.gleam @@ -7,14 +7,14 @@ pub type PacketType { Command } -fn packet_type_from_int(val: Int) -> PacketType { +pub fn packet_type_from_int(val: Int) -> PacketType { case val { 1 -> Command _ -> Telemetry } } -fn packet_type_to_int(val: PacketType) -> Int { +pub fn packet_type_to_int(val: PacketType) -> Int { case val { Command -> 1 Telemetry -> 0 @@ -35,7 +35,7 @@ pub type SequenceFlags { Unsegmented } -fn sequence_flags_from_int(val: Int) -> SequenceFlags { +pub fn sequence_flags_from_int(val: Int) -> SequenceFlags { case val { 0b00 -> Continuation 0b01 -> FirstSegment @@ -44,7 +44,7 @@ fn sequence_flags_from_int(val: Int) -> SequenceFlags { } } -fn sequence_flags_to_int(val: SequenceFlags) -> Int { +pub fn sequence_flags_to_int(val: SequenceFlags) -> Int { case val { Continuation -> 0b00 FirstSegment -> 0b01 @@ -53,7 +53,7 @@ fn sequence_flags_to_int(val: SequenceFlags) -> Int { } } -pub fn seq_flag_to_string(sp_seq_flags: SequenceFlags) -> String { +pub fn sequence_flags_to_string(sp_seq_flags: SequenceFlags) -> String { case sp_seq_flags { Continuation -> "Continuation" FirstSegment -> "First Segment" @@ -93,7 +93,7 @@ pub fn to_string(hdr: PrimaryHeader) -> String { |> string_tree.append(int.to_base16(hdr.apid)) |> string_tree.append("\n") |> string_tree.append("Sequence Flags: ") - |> string_tree.append(seq_flag_to_string(hdr.seq_flag)) + |> string_tree.append(sequence_flags_to_string(hdr.seq_flag)) |> string_tree.append("\n") |> string_tree.append("Sequence Count: ") |> string_tree.append(int.to_string(hdr.seq_count)) diff --git a/test/ccsds_space_packet_test.gleam b/test/ccsds_space_packet_test.gleam index 3831e7a..ecd12ad 100644 --- a/test/ccsds_space_packet_test.gleam +++ b/test/ccsds_space_packet_test.gleam @@ -1,12 +1,5 @@ import gleeunit -import gleeunit/should pub fn main() { gleeunit.main() } - -// gleeunit test functions end in `_test` -pub fn hello_world_test() { - 1 - |> should.equal(1) -} diff --git a/test/space_packet/primary_hdr_test.gleam b/test/space_packet/primary_hdr_test.gleam new file mode 100644 index 0000000..867149d --- /dev/null +++ b/test/space_packet/primary_hdr_test.gleam @@ -0,0 +1,120 @@ +import gleeunit/should +import space_packet/primary_hdr + +pub fn packet_type_from_int_test() { + primary_hdr.packet_type_from_int(0) + |> should.equal(primary_hdr.Telemetry) + + primary_hdr.packet_type_from_int(1) + |> should.equal(primary_hdr.Command) + + primary_hdr.packet_type_from_int(555) + |> should.equal(primary_hdr.Telemetry) +} + +pub fn packet_type_to_int_test() { + primary_hdr.packet_type_to_int(primary_hdr.Command) + |> should.equal(1) + + primary_hdr.packet_type_to_int(primary_hdr.Telemetry) + |> should.equal(0) +} + +pub fn packet_type_to_string() { + primary_hdr.packet_type_to_string(primary_hdr.Command) + |> should.equal("Command") + + primary_hdr.packet_type_to_string(primary_hdr.Telemetry) + |> should.equal("Telemetry") +} + +pub fn sequence_flags_from_int_test() { + primary_hdr.sequence_flags_from_int(0b00) + |> should.equal(primary_hdr.Continuation) + + primary_hdr.sequence_flags_from_int(0b01) + |> should.equal(primary_hdr.FirstSegment) + + primary_hdr.sequence_flags_from_int(0b10) + |> should.equal(primary_hdr.LastSegment) + + primary_hdr.sequence_flags_from_int(0b11) + |> should.equal(primary_hdr.Unsegmented) +} + +pub fn sequence_flags_to_int_test() { + primary_hdr.sequence_flags_to_int(primary_hdr.Continuation) + |> should.equal(0b00) + + primary_hdr.sequence_flags_to_int(primary_hdr.FirstSegment) + |> should.equal(0b01) + + primary_hdr.sequence_flags_to_int(primary_hdr.LastSegment) + |> should.equal(0b10) + + primary_hdr.sequence_flags_to_int(primary_hdr.Unsegmented) + |> should.equal(0b11) +} + +pub fn sequence_flags_to_string_test() { + primary_hdr.sequence_flags_to_string(primary_hdr.Continuation) + |> should.equal("Continuation") + + primary_hdr.sequence_flags_to_string(primary_hdr.FirstSegment) + |> should.equal("First Segment") + + primary_hdr.sequence_flags_to_string(primary_hdr.LastSegment) + |> should.equal("Last Segment") + + primary_hdr.sequence_flags_to_string(primary_hdr.Unsegmented) + |> should.equal("Unsegmented") +} + +pub fn from_bit_array_test() { + let valid_bit_array = << + 0b000:size(3), 0b0:size(1), 0b0:size(1), 0x200:size(11), 0b11:size(2), + 0x01:size(11), 0x01:size(16), + >> + let expected_hdr = + primary_hdr.PrimaryHeader( + version: 0, + sp_type: primary_hdr.Telemetry, + sec_hdr_flag: False, + apid: 0x200, + seq_flag: primary_hdr.Unsegmented, + seq_count: 1, + packet_len: 1, + ) + + primary_hdr.from_bit_array(valid_bit_array) + |> should.be_ok() + |> should.equal(expected_hdr) +} + +pub fn from_bit_array_fail_test() { + let invalid_bit_array = <<0x00:size(41)>> + + primary_hdr.from_bit_array(invalid_bit_array) + |> should.be_error() + |> should.equal(primary_hdr.InvalidSize) +} + +pub fn to_bit_array_test() { + let expected_bit_array = << + 0b000:size(3), 0b0:size(1), 0b0:size(1), 0x200:size(11), 0b11:size(2), + 0x01:size(11), 0x01:size(16), + >> + let hdr = + primary_hdr.PrimaryHeader( + version: 0, + sp_type: primary_hdr.Telemetry, + sec_hdr_flag: False, + apid: 0x200, + seq_flag: primary_hdr.Unsegmented, + seq_count: 1, + packet_len: 1, + ) + + primary_hdr.to_bit_array(hdr) + |> should.equal(expected_bit_array) +}