Feat[testing]: Add visual output of test results

This commit is contained in:
kayos@tcp.direct 2023-05-03 23:51:06 -07:00
parent 62ba0506f5
commit 914db3e84f
Signed by: kayos
GPG Key ID: 4B841471B4BEE979

@ -1,9 +1,16 @@
package bitstream
import (
"fmt"
"image/color"
"os"
"os/exec"
"testing"
"git.tcp.direct/kayos/common/entropy"
svg "github.com/ajstarks/svgo"
"git.tcp.direct/kayos/fla5h/pkg/pools"
)
func randomRGB() (r, g, b int) {
@ -14,21 +21,63 @@ func randomRGB() (r, g, b int) {
}
func TestPackColorLossy(t *testing.T) {
r := 0
g := 204
b := 255
check(r, g, b, t)
for i := 0; i < 100; i++ {
r, g, b = randomRGB()
var r, g, b int
colorMap := newCmap()
t.Run("sanity check", func(t *testing.T) {
r = 0
g = 204
b = 255
check(r, g, b, t)
}
})
t.Run("random", func(t *testing.T) {
for i := 0; i < 100; i++ {
r, g, b = randomRGB()
rn, gn, bn := check(r, g, b, t)
colorMap.add(r, g, b, rn, gn, bn)
}
renderSVG(colorMap)
})
}
func check(r, g, b int, t *testing.T) {
type cmap map[color.RGBA]color.RGBA
func newCmap() cmap {
return make(cmap)
}
func (c cmap) add(or, og, ob, ur, ug, ub int) {
c[color.RGBA{R: uint8(or), G: uint8(og), B: uint8(ob), A: 255}] =
color.RGBA{R: uint8(ur), G: uint8(ug), B: uint8(ub), A: 255}
}
func renderSVG(cmap map[color.RGBA]color.RGBA) {
canvasWriter := pools.Buffers.Get()
defer pools.Buffers.MustPut(canvasWriter)
canvas := svg.New(canvasWriter)
canvas.Start(1000, 10000)
canvas.Rect(0, 0, 1000, 1000, "fill:none;stroke:none;stroke-width:1")
offset := 0
for original, unpacked := range cmap {
// display original color next to unpacked color
canvas.Rect(0, offset, 250, 100,
fmt.Sprintf("fill:rgb(%d,%d,%d)", original.R, original.G, original.B))
canvas.Rect(250, offset, 250, 100,
fmt.Sprintf("fill:rgb(%d,%d,%d)", unpacked.R, unpacked.G, unpacked.B))
offset += 100
}
canvas.End()
if err := os.WriteFile("test.svg", canvasWriter.Bytes(), 0644); err != nil {
panic(err)
}
_ = exec.Command("nsxiv", "test.svg").Run()
}
func check(r, g, b int, t *testing.T) (rn, gn, bn int) {
t.Logf("Original: R:%d G:%d B:%d", r, g, b)
packed := PackColorLossy(r, g, b)
t.Logf("Packed: %d", packed)
rn, gn, bn := UnpackColorLossy(packed)
rn, gn, bn = UnpackColorLossy(packed)
t.Logf("Unpacked: R:%d G:%d B:%d", rn, gn, bn)
comp := map[string]map[int]int{
"r": {r: rn},
@ -54,4 +103,5 @@ func check(r, g, b int, t *testing.T) {
t.Errorf("lossy beyond tolerace: %d", totalDiff)
}
t.Logf("TotalDiff: %d", totalDiff)
return
}