103 lines
2.0 KiB
Go
103 lines
2.0 KiB
Go
package validator
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestCalculateInbound(t *testing.T) {
|
|
testPixel(t, 16, 12, 196, 214, 255)
|
|
testPixel(t, 14, 33, 120, 99, 50)
|
|
testPixel(t, 11, 11, 187, 205, 247)
|
|
testPixel(t, 7, 39, 255, 194, 50)
|
|
testPixel(t, 66, 26, 255, 194, 50)
|
|
testPixel(t, 27, 28, 95, 81, 47)
|
|
}
|
|
|
|
func TestCalculateOutbound(t *testing.T) {
|
|
testPixel(t, -20, 20, 255, 194, 50)
|
|
testPixel(t, 100, 100, 255, 194, 50)
|
|
testPixel(t, -100, 100, 255, 194, 50)
|
|
testPixel(t, -100, -100, 0, 0, 0)
|
|
testPixel(t, 100, -100, 0, 0, 0)
|
|
testPixel(t, 100, 10, 255, 255, 255)
|
|
}
|
|
|
|
func TestCalculateImage(t *testing.T) {
|
|
f, err := os.Open("./values.txt")
|
|
if err != nil {
|
|
t.Error("Test file does not exist or not readable")
|
|
}
|
|
defer f.Close()
|
|
|
|
br := bufio.NewScanner(f)
|
|
br.Split(bufio.ScanWords)
|
|
for y := 0; y < 40; y++ {
|
|
for x := 0; x < 71; x++ {
|
|
br.Scan()
|
|
expR := toInt(br.Bytes())
|
|
br.Scan()
|
|
expG := toInt(br.Bytes())
|
|
br.Scan()
|
|
expB := toInt(br.Bytes())
|
|
testPixel(t, x, y, expR, expG, expB)
|
|
}
|
|
}
|
|
}
|
|
|
|
func toInt(buf []byte) (n int) {
|
|
for _, v := range buf {
|
|
n = n*10 + int(v-'0')
|
|
}
|
|
return
|
|
}
|
|
|
|
func testPixel(t *testing.T, x int, y int, expR int, expG int, expB int) {
|
|
val := NewValidator(false)
|
|
r, g, b := val.Calculate(x, y).GetRGB()
|
|
t.Logf("Calculated (%d, %d)", x, y)
|
|
if r != expR {
|
|
t.Errorf("R = %d; want %d", r, expR)
|
|
}
|
|
if g != expG {
|
|
t.Errorf("G = %d; want %d", g, expG)
|
|
}
|
|
if b != expB {
|
|
t.Errorf("B = %d; want %d", b, expB)
|
|
}
|
|
}
|
|
|
|
func TestModFunction(t *testing.T) {
|
|
{
|
|
got := mod(100, 1)
|
|
if got != 10 {
|
|
t.Errorf("mod(100, 1) = 10; got %d", got)
|
|
}
|
|
}
|
|
{
|
|
got := mod(123456, 3)
|
|
if got != 123 {
|
|
t.Errorf("mod(123456, 3) = 123; got %d", got)
|
|
}
|
|
}
|
|
{
|
|
got := mod(5454, 2)
|
|
if got != 55 {
|
|
t.Errorf("mod(5454, 2) = 55; got %d", got)
|
|
}
|
|
}
|
|
{
|
|
got := mod(1234*555, 4) + 2
|
|
if got != 70 {
|
|
t.Errorf("mod(%d, 4) + 2 = 70; got %d", 1234*555, got)
|
|
}
|
|
}
|
|
{
|
|
got := mod(1234*556, 4) + 2
|
|
if got != 71 {
|
|
t.Errorf("mod(%d, 4) + 2 = 71; got %d", 1234*556, got)
|
|
}
|
|
}
|
|
}
|