]> gitweb.factorcode.org Git - factor.git/commitdiff
bzip3: add unit tests, fix unit test problems
authorrazetime <raghuallthetime@hotmail.com>
Mon, 31 Oct 2022 09:46:15 +0000 (15:16 +0530)
committerrazetime <raghuallthetime@hotmail.com>
Mon, 31 Oct 2022 09:46:15 +0000 (15:16 +0530)
extra/compression/bzip3/bzip3-tests.factor [new file with mode: 0644]
extra/compression/bzip3/bzip3.factor

diff --git a/extra/compression/bzip3/bzip3-tests.factor b/extra/compression/bzip3/bzip3-tests.factor
new file mode 100644 (file)
index 0000000..e5ba69c
--- /dev/null
@@ -0,0 +1,28 @@
+! Copyright (C) 2022 Raghu Ranganathan.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test byte-arrays random locals sequences ;
+IN: compression.bzip3.tests
+
+! Simple compression and decompression
+{ B{
+    0 0 0 0 0 0 0 12 66 90 51 118 49 0 4 1 0 1 0 0 0 20 0 0 0 12
+    0 0 0 177 144 125 228 255 255 255 255 72 101 108 108 111 32
+    87 111 114 108 100 33 0 0 0
+} } [ B{ 72 101 108 108 111 32 87 111 114 108 100 33 } f compress ] unit-test
+{ B{
+  72 101 108 108 111 32 87 111 114 108 100 33 
+} } [ B{
+  0 0 0 0 0 0 0 12 66 90 51 118 49 0 4 1 0 1 0 0 0 20 0 0 0 12 0 0 0 177 144
+  125 228 255 255 255 255 72 101 108 108 111 32 87 111 114 108
+  100 33 0 0 0
+} decompress ] unit-test
+
+! Random byte sequences
+40 [| |
+  36 [ 256 random ] B{ } replicate-as :> input
+  { input } [ input f compress decompress ] unit-test
+] times
+
+! Incorrect block sizes must error
+[ B{ } 0 compress ] must-fail
+[ B{ } 536870912 compress ] must-fail
index 139239c58e9332dc89d62c656bbbaa5f8329842d..1a9a4dade55ae0b9cfc5b1fcdbd8657182ea45d0 100644 (file)
@@ -1,4 +1,4 @@
-USING: alien alien.libraries alien.c-types alien.data alien.syntax
+USING: alien alien.libraries alien.c-types alien.data alien.syntax endian
        kernel io.encodings.string byte-arrays sequences combinators syntax
        compression.bzip3.ffi locals math math.order summary pair-rocket ;
 IN: compression.bzip3
@@ -7,6 +7,7 @@ ERROR: invalid-block-size size ;
 M: invalid-block-size summary drop "Block size must be between 65 KiB and 511 MiB" ;
 ERROR: internal-error msg ;
 M: internal-error summary drop "bzip3: Internal Error" ;
+
 <PRIVATE
 CONSTANT: dsize 1048576 ! placeholder block size
 
@@ -35,13 +36,14 @@ ALIAS: version bz3_version
   out-size <byte-array> :> out
   block-size/f [ dsize ] unless* validate-block-size
   byte-array out in-size out-size size_t <ref> bz3_compress
-  dup 0 = [ drop out ] [ throw-internal-error ] if
+  dup 0 = [ drop in-size 8 >be out append ] [ throw-internal-error ] if
 ;
 
 :: decompress ( byte-array -- byte-array' )
-  byte-array length :> in-size
-  in-size bz3_bound :> out-size
+  byte-array 8 cut-slice :> ( head in )
+  in length :> in-size
+  head be> :> out-size
   out-size <byte-array> :> out
-  byte-array out in-size out-size size_t <ref> bz3_decompress
+  in out in-size out-size size_t <ref> bz3_decompress
   dup 0 = [ drop out ] [ throw-internal-error ] if
 ;