aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRichard Viney <richard.viney@gmail.com>2024-11-28 11:15:42 +1300
committerLouis Pilfold <louis@lpil.uk>2024-11-28 12:20:57 +0000
commit406eb387d750191546d9da6f54df50b4b1ee8409 (patch)
tree845945efea54c443eaf63eb7bd028ccb8e650f1a /test
parent4411f584ff87f7acdf26676fc085f4b277eff166 (diff)
downloadgleam_stdlib-406eb387d750191546d9da6f54df50b4b1ee8409.tar.gz
gleam_stdlib-406eb387d750191546d9da6f54df50b4b1ee8409.zip
Add bit_array.pad_to_bytes. Pad bit arrays when encoding and in bytes_tree.
Diffstat (limited to 'test')
-rw-r--r--test/gleam/bit_array_test.gleam131
-rw-r--r--test/gleam/bytes_tree_test.gleam24
2 files changed, 150 insertions, 5 deletions
diff --git a/test/gleam/bit_array_test.gleam b/test/gleam/bit_array_test.gleam
index 555c61a..493e696 100644
--- a/test/gleam/bit_array_test.gleam
+++ b/test/gleam/bit_array_test.gleam
@@ -36,11 +36,50 @@ pub fn bit_size_erlang_only_test() {
}
pub fn byte_size_test() {
- bit_array.byte_size(bit_array.from_string("hello"))
+ bit_array.byte_size(<<>>)
+ |> should.equal(0)
+
+ bit_array.byte_size(<<0, 1, 2, 3, 4>>)
|> should.equal(5)
+}
- bit_array.byte_size(bit_array.from_string(""))
- |> should.equal(0)
+// This test is target specific since it's using non byte-aligned BitArrays
+// and those are not supported on the JavaScript target.
+@target(erlang)
+pub fn byte_size_erlang_only_test() {
+ bit_array.byte_size(<<1, 2, 3:6>>)
+ |> should.equal(3)
+}
+
+pub fn pad_to_bytes_test() {
+ <<>>
+ |> bit_array.pad_to_bytes
+ |> should.equal(<<>>)
+
+ <<0xAB>>
+ |> bit_array.pad_to_bytes
+ |> should.equal(<<0xAB>>)
+
+ <<0xAB, 0x12>>
+ |> bit_array.pad_to_bytes
+ |> should.equal(<<0xAB, 0x12>>)
+}
+
+// This test is target specific since it's using non byte-aligned BitArrays
+// and those are not supported on the JavaScript target.
+@target(erlang)
+pub fn pad_to_bytes_erlang_only_test() {
+ <<1:1>>
+ |> bit_array.pad_to_bytes
+ |> should.equal(<<0x80>>)
+
+ <<-1:7>>
+ |> bit_array.pad_to_bytes
+ |> should.equal(<<0xFE>>)
+
+ <<0xAB, 0x12, 3:3>>
+ |> bit_array.pad_to_bytes
+ |> should.equal(<<0xAB, 0x12, 0x60>>)
}
pub fn not_equal_test() {
@@ -85,9 +124,25 @@ pub fn concat_test() {
// and those are not supported on the JavaScript target.
@target(erlang)
pub fn concat_erlang_only_test() {
+ [<<-1:32>>, <<0:1>>, <<0:0>>]
+ |> bit_array.concat
+ |> should.equal(<<255, 255, 255, 255, 0:1>>)
+
+ [<<-20:6, 2>>, <<3:4>>, <<7:3>>, <<-1:64>>]
+ |> bit_array.concat
+ |> should.equal(<<176, 8, 255, 255, 255, 255, 255, 255, 255, 255, 31:size(5)>>)
+
[<<1, 2:4>>, <<3>>]
|> bit_array.concat
|> should.equal(<<1, 2:4, 3>>)
+
+ [<<-1:32>>, <<0:1>>, <<0:0>>]
+ |> bit_array.concat
+ |> should.equal(<<255, 255, 255, 255, 0:1>>)
+
+ [<<-20:6, 2>>, <<3:4>>, <<7:3>>, <<-1:64>>]
+ |> bit_array.concat
+ |> should.equal(<<176, 8, 255, 255, 255, 255, 255, 255, 255, 255, 31:size(5)>>)
}
pub fn slice_test() {
@@ -133,6 +188,19 @@ pub fn slice_test() {
|> should.equal(Ok(<<"b":utf8>>))
}
+// This test is target specific since it's using non byte-aligned BitArrays
+// and those are not supported on the JavaScript target.
+@target(erlang)
+pub fn slice_erlang_onyl_test() {
+ <<0, 1, 2:7>>
+ |> bit_array.slice(0, 3)
+ |> should.equal(Error(Nil))
+
+ <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>>
+ |> bit_array.slice(8, 12)
+ |> should.equal(Error(Nil))
+}
+
pub fn to_string_test() {
<<>>
|> bit_array.to_string
@@ -155,6 +223,15 @@ pub fn to_string_test() {
|> should.equal(Error(Nil))
}
+// This test is target specific since it's using non byte-aligned BitArrays
+// and those are not supported on the JavaScript target.
+@target(erlang)
+pub fn to_string_erlang_only_test() {
+ <<"ΓΈ":utf8, 50:4>>
+ |> bit_array.to_string
+ |> should.equal(Error(Nil))
+}
+
pub fn is_utf8_test() {
<<>>
|> bit_array.is_utf8
@@ -207,6 +284,23 @@ pub fn base64_encode_test() {
))
}
+// This test is target specific since it's using non byte-aligned BitArrays
+// and those are not supported on the JavaScript target.
+@target(erlang)
+pub fn base64_erlang_only_encode_test() {
+ <<-1:7>>
+ |> bit_array.base64_encode(True)
+ |> should.equal("/g==")
+
+ <<0xFA, 5:3>>
+ |> bit_array.base64_encode(True)
+ |> should.equal("+qA=")
+
+ <<0xFA, 0xBC, 0x6D, 1:1>>
+ |> bit_array.base64_encode(True)
+ |> should.equal("+rxtgA==")
+}
+
pub fn base64_decode_test() {
"/3/+/A=="
|> bit_array.base64_decode()
@@ -305,6 +399,27 @@ pub fn base16_test() {
|> should.equal("A1B2C3D4E5F67891")
}
+// This test is target specific since it's using non byte-aligned BitArrays
+// and those are not supported on the JavaScript target.
+@target(erlang)
+pub fn base16_encode_erlang_only_test() {
+ <<-1:7>>
+ |> bit_array.base16_encode()
+ |> should.equal("FE")
+
+ <<0xFA, 5:3>>
+ |> bit_array.base16_encode()
+ |> should.equal("FAA0")
+
+ <<0xFA, 5:4>>
+ |> bit_array.base16_encode()
+ |> should.equal("FA50")
+
+ <<0xFA, 0xBC, 0x6D, 1:1>>
+ |> bit_array.base16_encode()
+ |> should.equal("FABC6D80")
+}
+
pub fn base16_decode_test() {
bit_array.base16_decode("")
|> should.equal(Ok(<<>>))
@@ -353,7 +468,7 @@ pub fn inspect_test() {
// This test is target specific since it's using non byte-aligned BitArrays
// and those are not supported on the JavaScript target.
@target(erlang)
-pub fn inspect_partial_bytes_test() {
+pub fn inspect_erlang_only_test() {
bit_array.inspect(<<4:5>>)
|> should.equal("<<4:size(5)>>")
@@ -365,7 +480,7 @@ pub fn inspect_partial_bytes_test() {
}
@target(erlang)
-pub fn compare_different_sizes_test() {
+pub fn compare_test() {
bit_array.compare(<<4:5>>, <<4:5>>)
|> should.equal(order.Eq)
@@ -458,4 +573,10 @@ pub fn starts_with_erlang_only_test() {
bit_array.starts_with(<<0:127>>, <<1:127>>)
|> should.be_false
+
+ bit_array.starts_with(<<0xFF, 0x81>>, <<0xFF, 1:1>>)
+ |> should.be_true
+
+ bit_array.starts_with(<<0xFF, 0x81>>, <<0xFF, 0:1>>)
+ |> should.be_false
}
diff --git a/test/gleam/bytes_tree_test.gleam b/test/gleam/bytes_tree_test.gleam
index 1f7245a..3f549f1 100644
--- a/test/gleam/bytes_tree_test.gleam
+++ b/test/gleam/bytes_tree_test.gleam
@@ -18,6 +18,23 @@ pub fn tree_test() {
|> should.equal(4)
}
+@target(erlang)
+pub fn tree_unaligned_bit_arrays_test() {
+ let data =
+ bytes_tree.from_bit_array(<<-1:5>>)
+ |> bytes_tree.append(<<-1:3>>)
+ |> bytes_tree.append(<<-2:2>>)
+ |> bytes_tree.prepend(<<-1:4>>)
+
+ data
+ |> bytes_tree.to_bit_array
+ |> should.equal(<<-1:4, 0:4, -1:5, 0:3, -1:3, 0:5, -2:2, 0:6>>)
+
+ data
+ |> bytes_tree.byte_size
+ |> should.equal(4)
+}
+
pub fn tree_with_strings_test() {
let data =
bytes_tree.from_bit_array(<<1>>)
@@ -67,6 +84,13 @@ pub fn concat_bit_arrays_test() {
|> should.equal(<<"hey":utf8>>)
}
+@target(erlang)
+pub fn concat_unaligned_bit_arrays_test() {
+ bytes_tree.concat_bit_arrays([<<-1:4>>, <<-1:5>>, <<-1:3>>, <<-2:2>>])
+ |> bytes_tree.to_bit_array
+ |> should.equal(<<-1:4, 0:4, -1:5, 0:3, -1:3, 0:5, -2:2, 0:6>>)
+}
+
pub fn from_bit_array() {
// Regression test: no additional modification of the tree
bytes_tree.from_bit_array(<<>>)