From: razetime Date: Mon, 31 Oct 2022 09:46:15 +0000 (+0530) Subject: bzip3: add unit tests, fix unit test problems X-Git-Tag: 0.99~1042^2~1 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=bd72509777804f99727b12873fc9dac546970cfb bzip3: add unit tests, fix unit test problems --- diff --git a/extra/compression/bzip3/bzip3-tests.factor b/extra/compression/bzip3/bzip3-tests.factor new file mode 100644 index 0000000000..e5ba69c6e9 --- /dev/null +++ b/extra/compression/bzip3/bzip3-tests.factor @@ -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 diff --git a/extra/compression/bzip3/bzip3.factor b/extra/compression/bzip3/bzip3.factor index 139239c58e..1a9a4dade5 100644 --- a/extra/compression/bzip3/bzip3.factor +++ b/extra/compression/bzip3/bzip3.factor @@ -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" ; + :> out block-size/f [ dsize ] unless* validate-block-size byte-array out in-size out-size size_t 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 :> out - byte-array out in-size out-size size_t bz3_decompress + in out in-size out-size size_t bz3_decompress dup 0 = [ drop out ] [ throw-internal-error ] if ;