Merge pull request #1136 from Kelimion/compress_tests

Beginning of CI tests for PNG, GZIP and ZLIB
This commit is contained in:
Jeroen van Rijn
2021-09-07 18:37:55 +02:00
committed by GitHub
6 changed files with 2155 additions and 2 deletions
+7 -2
View File
@@ -69,7 +69,12 @@ jobs:
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat
cd core\math\big\tests
python3 --version
call build.bat
timeout-minutes: 10
- name: core:image and core:compress tests
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat
cd tests\core
call build.bat
timeout-minutes: 10
+2
View File
@@ -0,0 +1,2 @@
*.zip
*.png
+6
View File
@@ -0,0 +1,6 @@
@echo off
set COMMON=-show-timings -no-bounds-check -vet -strict-style
set PATH_TO_ODIN==..\..\odin
python3 download_assets.py
%PATH_TO_ODIN% test image %COMMON%
%PATH_TO_ODIN% test compress %COMMON%
+120
View File
@@ -0,0 +1,120 @@
package test_core_compress
/*
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-3 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
A test suite for ZLIB, GZIP and PNG.
*/
import "core:testing"
import "core:compress/zlib"
import "core:compress/gzip"
import "core:bytes"
import "core:fmt"
import "core:mem"
import "core:os"
import "core:io"
when ODIN_TEST {
expect :: testing.expect
} else {
expect :: proc(t: ^testing.T, condition: bool, message: string) {
if !condition {
fmt.println(message)
}
}
}
main :: proc() {
w, _ := io.to_writer(os.stream_from_handle(os.stdout))
t := testing.T{w=w}
zlib_test(&t)
gzip_test(&t)
}
@test
zlib_test :: proc(t: ^testing.T) {
ODIN_DEMO := []u8{
120, 156, 101, 144, 77, 110, 131, 48, 16, 133, 215, 204, 41, 158, 44,
69, 73, 32, 148, 182, 75, 35, 14, 208, 125, 47, 96, 185, 195, 143,
130, 13, 50, 38, 81, 84, 101, 213, 75, 116, 215, 43, 246, 8, 53,
82, 126, 8, 181, 188, 152, 153, 111, 222, 147, 159, 123, 165, 247, 170,
98, 24, 213, 88, 162, 198, 244, 157, 243, 16, 186, 115, 44, 75, 227,
5, 77, 115, 72, 137, 222, 117, 122, 179, 197, 39, 69, 161, 170, 156,
50, 144, 5, 68, 130, 4, 49, 126, 127, 190, 191, 144, 34, 19, 57,
69, 74, 235, 209, 140, 173, 242, 157, 155, 54, 158, 115, 162, 168, 12,
181, 239, 246, 108, 17, 188, 174, 242, 224, 20, 13, 199, 198, 235, 250,
194, 166, 129, 86, 3, 99, 157, 172, 37, 230, 62, 73, 129, 151, 252,
70, 211, 5, 77, 31, 104, 188, 160, 113, 129, 215, 59, 205, 22, 52,
123, 160, 83, 142, 255, 242, 89, 123, 93, 149, 200, 50, 188, 85, 54,
252, 18, 248, 192, 238, 228, 235, 198, 86, 224, 118, 224, 176, 113, 166,
112, 67, 106, 227, 159, 122, 215, 88, 95, 110, 196, 123, 205, 183, 224,
98, 53, 8, 104, 213, 234, 201, 147, 7, 248, 192, 14, 170, 29, 25,
171, 15, 18, 59, 138, 112, 63, 23, 205, 110, 254, 136, 109, 78, 231,
63, 234, 138, 133, 204,
}
buf: bytes.Buffer
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track)
err := zlib.inflate(ODIN_DEMO, &buf)
expect(t, err == nil, "ZLIB failed to decompress ODIN_DEMO")
s := bytes.buffer_to_string(&buf)
expect(t, s[68] == 240 && s[69] == 159 && s[70] == 152, "ZLIB result should've contained 😃 at position 68.")
expect(t, len(s) == 438, "ZLIB result has an unexpected length.")
bytes.buffer_destroy(&buf)
for _, v in track.allocation_map {
error := fmt.tprintf("ZLIB test leaked %v bytes", v.size)
expect(t, false, error)
}
}
@test
gzip_test :: proc(t: ^testing.T) {
// Small GZIP file with fextra, fname and fcomment present.
TEST: []u8 = {
0x1f, 0x8b, 0x08, 0x1c, 0xcb, 0x3b, 0x3a, 0x5a,
0x02, 0x03, 0x07, 0x00, 0x61, 0x62, 0x03, 0x00,
0x63, 0x64, 0x65, 0x66, 0x69, 0x6c, 0x65, 0x6e,
0x61, 0x6d, 0x65, 0x00, 0x54, 0x68, 0x69, 0x73,
0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x63, 0x6f,
0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x2b, 0x48,
0xac, 0xcc, 0xc9, 0x4f, 0x4c, 0x01, 0x00, 0x15,
0x6a, 0x2c, 0x42, 0x07, 0x00, 0x00, 0x00,
}
buf: bytes.Buffer
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track)
err := gzip.load(TEST, &buf) // , 438);
expect(t, err == nil, "GZIP failed to decompress TEST")
s := bytes.buffer_to_string(&buf)
expect(t, s == "payload", "GZIP result wasn't 'payload'")
bytes.buffer_destroy(&buf)
for _, v in track.allocation_map {
error := fmt.tprintf("GZIP test leaked %v bytes", v.size)
expect(t, false, error)
}
}
+98
View File
@@ -0,0 +1,98 @@
#!/usr/bin/env python3
import urllib.request
import shutil
import sys
import os
import zipfile
DOWNLOAD_BASE_PATH = "assets/{}"
ASSETS_BASE_URL = "https://raw.githubusercontent.com/Kelimion/compress-odin/master/tests/assets/{}/{}"
PNG_IMAGES = [
"basi0g01.png", "basi0g02.png", "basi0g04.png", "basi0g08.png", "basi0g16.png", "basi2c08.png",
"basi2c16.png", "basi3p01.png", "basi3p02.png", "basi3p04.png", "basi3p08.png", "basi4a08.png",
"basi4a16.png", "basi6a08.png", "basi6a16.png", "basn0g01.png", "basn0g02.png", "basn0g04.png",
"basn0g08.png", "basn0g16.png", "basn2c08.png", "basn2c16.png", "basn3p01.png", "basn3p02.png",
"basn3p04.png", "basn3p08.png", "basn4a08.png", "basn4a16.png", "basn6a08.png", "basn6a16.png",
"bgai4a08.png", "bgai4a16.png", "bgan6a08.png", "bgan6a16.png", "bgbn4a08.png", "bggn4a16.png",
"bgwn6a08.png", "bgyn6a16.png", "ccwn2c08.png", "ccwn3p08.png", "cdfn2c08.png", "cdhn2c08.png",
"cdsn2c08.png", "cdun2c08.png", "ch1n3p04.png", "ch2n3p08.png", "cm0n0g04.png", "cm7n0g04.png",
"cm9n0g04.png", "cs3n2c16.png", "cs3n3p08.png", "cs5n2c08.png", "cs5n3p08.png", "cs8n2c08.png",
"cs8n3p08.png", "ct0n0g04.png", "ct1n0g04.png", "cten0g04.png", "ctfn0g04.png", "ctgn0g04.png",
"cthn0g04.png", "ctjn0g04.png", "ctzn0g04.png", "exif2c08.png", "f00n0g08.png", "f00n2c08.png",
"f01n0g08.png", "f01n2c08.png", "f02n0g08.png", "f02n2c08.png", "f03n0g08.png", "f03n2c08.png",
"f04n0g08.png", "f04n2c08.png", "f99n0g04.png", "g03n0g16.png", "g03n2c08.png", "g03n3p04.png",
"g04n0g16.png", "g04n2c08.png", "g04n3p04.png", "g05n0g16.png", "g05n2c08.png", "g05n3p04.png",
"g07n0g16.png", "g07n2c08.png", "g07n3p04.png", "g10n0g16.png", "g10n2c08.png", "g10n3p04.png",
"g25n0g16.png", "g25n2c08.png", "g25n3p04.png", "oi1n0g16.png", "oi1n2c16.png", "oi2n0g16.png",
"oi2n2c16.png", "oi4n0g16.png", "oi4n2c16.png", "oi9n0g16.png", "oi9n2c16.png", "pp0n2c16.png",
"pp0n6a08.png", "ps1n0g08.png", "ps1n2c16.png", "ps2n0g08.png", "ps2n2c16.png", "s01i3p01.png",
"s01n3p01.png", "s02i3p01.png", "s02n3p01.png", "s03i3p01.png", "s03n3p01.png", "s04i3p01.png",
"s04n3p01.png", "s05i3p02.png", "s05n3p02.png", "s06i3p02.png", "s06n3p02.png", "s07i3p02.png",
"s07n3p02.png", "s08i3p02.png", "s08n3p02.png", "s09i3p02.png", "s09n3p02.png", "s32i3p04.png",
"s32n3p04.png", "s33i3p04.png", "s33n3p04.png", "s34i3p04.png", "s34n3p04.png", "s35i3p04.png",
"s35n3p04.png", "s36i3p04.png", "s36n3p04.png", "s37i3p04.png", "s37n3p04.png", "s38i3p04.png",
"s38n3p04.png", "s39i3p04.png", "s39n3p04.png", "s40i3p04.png", "s40n3p04.png", "tbbn0g04.png",
"tbbn2c16.png", "tbbn3p08.png", "tbgn2c16.png", "tbgn3p08.png", "tbrn2c08.png", "tbwn0g16.png",
"tbwn3p08.png", "tbyn3p08.png", "tm3n3p02.png", "tp0n0g08.png", "tp0n2c08.png", "tp0n3p08.png",
"tp1n3p08.png", "xc1n0g08.png", "xc9n2c08.png", "xcrn0g04.png", "xcsn0g01.png", "xd0n2c08.png",
"xd3n2c08.png", "xd9n2c08.png", "xdtn0g01.png", "xhdn0g08.png", "xlfn0g04.png", "xs1n0g01.png",
"xs2n0g01.png", "xs4n0g01.png", "xs7n0g01.png", "z00n2c08.png", "z03n2c08.png", "z06n2c08.png",
"z09n2c08.png",
"PngSuite.png", "logo-slim.png", "emblem-1024.png"
]
def try_download_file(url, out_file):
try:
with urllib.request.urlopen(url) as response, open(out_file, 'wb') as of:
shutil.copyfileobj(response, of)
print("... ", out_file)
except urllib.error.HTTPError:
print("Could not download", url)
return 1
def try_download_and_unpack_zip(suite):
url = ASSETS_BASE_URL.format(suite, "{}.zip".format(suite))
out_file = DOWNLOAD_BASE_PATH.format(suite) + "/{}.zip".format(suite)
print("\tDownloading {} to {}.".format(url, out_file))
if try_download_file(url, out_file) is not None:
print("Could not download ZIP file")
return 1
# Try opening the ZIP file and extracting the test images
try:
with zipfile.ZipFile(out_file) as z:
for file in z.filelist:
filename = file.filename
extract_path = DOWNLOAD_BASE_PATH.format(suite)
print("\t\tExtracting: {}".format(filename))
z.extract(file, extract_path)
except:
print("Could not extract ZIP file")
return 2
def main():
print("Downloading PNG assets")
# Make PNG assets path
try:
path = DOWNLOAD_BASE_PATH.format("PNG")
os.makedirs(path)
except FileExistsError:
pass
# Try downloading and unpacking the PNG assets
r = try_download_and_unpack_zip("PNG")
if r is not None:
return r
# We could fall back on downloading the PNG files individually, but it's slow
print("Done downloading PNG assets")
return 0
if __name__ == '__main__':
sys.exit(main())
File diff suppressed because it is too large Load Diff