[compress/shoco] Add short string compressor.

This commit is contained in:
Jeroen van Rijn
2022-04-22 16:55:47 +02:00
parent b4f8efcbe6
commit e799476f90
8 changed files with 645 additions and 1 deletions
+26
View File
@@ -0,0 +1,26 @@
Copyright (c) 2016-2021 Ginger Bill. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Binary file not shown.
+95
View File
@@ -0,0 +1,95 @@
<p align="center">
<img src="misc/logo-slim.png" alt="Odin logo" style="width:65%">
<br/>
The Data-Oriented Language for Sane Software Development.
<br/>
<br/>
<a href="https://github.com/odin-lang/odin/releases/latest">
<img src="https://img.shields.io/github/release/odin-lang/odin.svg">
</a>
<a href="https://github.com/odin-lang/odin/releases/latest">
<img src="https://img.shields.io/badge/platforms-Windows%20|%20Linux%20|%20macOS-green.svg">
</a>
<br>
<a href="https://discord.gg/odinlang">
<img src="https://img.shields.io/discord/568138951836172421?logo=discord">
</a>
<a href="https://github.com/odin-lang/odin/actions">
<img src="https://github.com/odin-lang/odin/workflows/CI/badge.svg?branch=master&event=push">
</a>
</p>
# The Odin Programming Language
Odin is a general-purpose programming language with distinct typing, built for high performance, modern systems, and built-in data-oriented data types. The Odin Programming Language, the C alternative for the joy of programming.
Website: [https://odin-lang.org/](https://odin-lang.org/)
```odin
package main
import "core:fmt"
main :: proc() {
program := "+ + * 😃 - /"
accumulator := 0
for token in program {
switch token {
case '+': accumulator += 1
case '-': accumulator -= 1
case '*': accumulator *= 2
case '/': accumulator /= 2
case '😃': accumulator *= accumulator
case: // Ignore everything else
}
}
fmt.printf("The program \"%s\" calculates the value %d\n",
program, accumulator)
}
```
## Documentation
#### [Getting Started](https://odin-lang.org/docs/install)
Instructions for downloading and installing the Odin compiler and libraries.
#### [Nightly Builds](https://odin-lang.org/docs/nightly/)
Get the latest nightly builds of Odin.
### Learning Odin
#### [Overview of Odin](https://odin-lang.org/docs/overview)
An overview of the Odin programming language.
#### [Frequently Asked Questions (FAQ)](https://odin-lang.org/docs/faq)
Answers to common questions about Odin.
#### [Packages](https://pkg.odin-lang.org/)
Documentation for all the official packages part of the [core](https://pkg.odin-lang.org/core/) and [vendor](https://pkg.odin-lang.org/vendor/) library collections.
#### [The Odin Wiki](https://github.com/odin-lang/Odin/wiki)
A wiki maintained by the Odin community.
#### [Odin Discord](https://discord.gg/sVBPHEv)
Get live support and talk with other odiners on the Odin Discord.
### Articles
#### [The Odin Blog](https://odin-lang.org/news/)
The official blog of the Odin programming language, featuring announcements, news, and in-depth articles by the Odin team and guests.
## Warnings
* The Odin compiler is still in development.
Binary file not shown.
+56 -1
View File
@@ -7,13 +7,14 @@ package test_core_compress
List of contributors:
Jeroen van Rijn: Initial implementation.
A test suite for ZLIB, GZIP.
A test suite for ZLIB, GZIP and Shoco.
*/
import "core:testing"
import "core:compress/zlib"
import "core:compress/gzip"
import "core:compress/shoco"
import "core:bytes"
import "core:fmt"
@@ -48,6 +49,7 @@ main :: proc() {
t := testing.T{w=w}
zlib_test(&t)
gzip_test(&t)
shoco_test(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
if TEST_fail > 0 {
@@ -134,3 +136,56 @@ gzip_test :: proc(t: ^testing.T) {
expect(t, false, error)
}
}
@test
shoco_test :: proc(t: ^testing.T) {
Shoco_Tests :: []struct{
compressed: []u8,
raw: []u8,
short_pack: int,
short_sentinel: int,
}{
{ #load("../assets/Shoco/README.md.shoco"), #load("../assets/Shoco/README.md"), 10, 1006 },
{ #load("../assets/Shoco/LICENSE.shoco"), #load("../assets/Shoco/LICENSE"), 25, 68 },
}
for v in Shoco_Tests {
expected_raw := len(v.raw)
expected_compressed := len(v.compressed)
biggest_unpacked := shoco.decompress_bound(expected_compressed)
biggest_packed := shoco.compress_bound(expected_raw)
buffer := make([]u8, max(biggest_packed, biggest_unpacked))
defer delete(buffer)
size, err := shoco.decompress(v.compressed, buffer[:])
msg := fmt.tprintf("Expected `decompress` to return `nil`, got %v", err)
expect(t, err == nil, msg)
msg = fmt.tprintf("Decompressed %v bytes into %v. Expected to decompress into %v bytes.", len(v.compressed), size, expected_raw)
expect(t, size == expected_raw, msg)
expect(t, string(buffer[:size]) == string(v.raw), "Decompressed contents don't match.")
size, err = shoco.compress(string(v.raw), buffer[:])
expect(t, err == nil, "Expected `compress` to return `nil`.")
msg = fmt.tprintf("Compressed %v bytes into %v. Expected to compress into %v bytes.", expected_raw, size, expected_compressed)
expect(t, size == expected_compressed, msg)
size, err = shoco.decompress(v.compressed, buffer[:expected_raw - 10])
msg = fmt.tprintf("Decompressing into too small a buffer returned %v, expected `.Output_Too_Short`", err)
expect(t, err == .Output_Too_Short, msg)
size, err = shoco.compress(string(v.raw), buffer[:expected_compressed - 10])
msg = fmt.tprintf("Compressing into too small a buffer returned %v, expected `.Output_Too_Short`", err)
expect(t, err == .Output_Too_Short, msg)
size, err = shoco.decompress(v.compressed[:v.short_pack], buffer[:])
expect(t, err == .Stream_Too_Short, "Expected `decompress` to return `Stream_Too_Short` because there was no more data after selecting a pack.")
size, err = shoco.decompress(v.compressed[:v.short_sentinel], buffer[:])
expect(t, err == .Stream_Too_Short, "Expected `decompress` to return `Stream_Too_Short` because there was no more data after non-ASCII sentinel.")
}
}