The cell and terminal list checks now run as one Ziran module whose SelfTest walks both case sets. The geometry suite's table runs its camera cases, which no script ran before, then the vector path and view transform behaviors. Each pair compiled through the same commands, so behavior scripts drop from 179 to 177 with the same assertions; the plan maps both removals.
529 lines
15 KiB
Ziran
529 lines
15 KiB
Ziran
|
|
#import "vector_path"
|
|
#import "geometry"
|
|
|
|
file_bytes: [2048]u8;
|
|
file_length: s32 = 0;
|
|
command_bytes: [2048]u8;
|
|
command_length: s32 = 0;
|
|
previous: [2]s32;
|
|
axis: s32 = 0;
|
|
coverage: [256 * 256]u8;
|
|
tile_coverage: [128 * 128]u8;
|
|
|
|
ResetFile :: () {
|
|
file_length = 0
|
|
}
|
|
|
|
Raw :: (value: u8) {
|
|
file_bytes[file_length] = value
|
|
file_length += 1
|
|
}
|
|
|
|
Varint :: (value: u64) {
|
|
remaining: u64 = value
|
|
while true {
|
|
byte: u8 = cast(u8)(remaining & cast(u64)127)
|
|
remaining >>= cast(u32)7
|
|
if remaining != 0 { byte |= cast(u8)128 }
|
|
Raw(byte)
|
|
if remaining == 0 { break }
|
|
}
|
|
}
|
|
|
|
CommandVarint :: (value: u64) {
|
|
remaining: u64 = value
|
|
while true {
|
|
byte: u8 = cast(u8)(remaining & cast(u64)127)
|
|
remaining >>= cast(u32)7
|
|
if remaining != 0 { byte |= cast(u8)128 }
|
|
CommandByte(byte)
|
|
if remaining == 0 { break }
|
|
}
|
|
}
|
|
|
|
Zigzag :: (value: s32) -> u64 {
|
|
if value < 0 {
|
|
magnitude: s32 = 0 - value
|
|
encoded: u64 = cast(u64)magnitude
|
|
encoded = encoded * 2 - 1
|
|
return encoded
|
|
} else {
|
|
encoded: u64 = cast(u64)cast(u32)value
|
|
return encoded * 2
|
|
}
|
|
}
|
|
|
|
StartFile :: (width: s32, height: s32, paths: s32) {
|
|
ResetFile()
|
|
Raw(#char "K")
|
|
Raw(#char "V")
|
|
Raw(#char "P")
|
|
Raw(#char "1")
|
|
Varint(cast(u64)width * 16)
|
|
Varint(cast(u64)height * 16)
|
|
Varint(cast(u64)paths)
|
|
}
|
|
|
|
StartPath :: (rule: u8) {
|
|
command_length = 0
|
|
previous[0] = 0
|
|
previous[1] = 0
|
|
axis = 0
|
|
Raw(rule)
|
|
}
|
|
|
|
CommandByte :: (value: u8) {
|
|
command_bytes[command_length] = value
|
|
command_length += 1
|
|
}
|
|
|
|
FinishPath :: () {
|
|
Varint(cast(u64)command_length)
|
|
index: s32 = 0
|
|
while index < command_length {
|
|
Raw(command_bytes[index])
|
|
index += 1
|
|
}
|
|
}
|
|
|
|
// Tests name absolute positions in 1/16 units; the format stores deltas per axis.
|
|
Coordinate :: (value: s32) {
|
|
CommandVarint(Zigzag(value - previous[axis]))
|
|
previous[axis] = value
|
|
axis = 1 - axis
|
|
}
|
|
|
|
MoveTo :: (x: s32, y: s32) {
|
|
CommandByte(#char "M")
|
|
Coordinate(x)
|
|
Coordinate(y)
|
|
}
|
|
|
|
LineTo :: (x: s32, y: s32) {
|
|
CommandByte(#char "L")
|
|
Coordinate(x)
|
|
Coordinate(y)
|
|
}
|
|
|
|
CurveTo :: (a: s32, b: s32, c: s32, d: s32, e: s32, f: s32) {
|
|
CommandByte(#char "C")
|
|
Coordinate(a)
|
|
Coordinate(b)
|
|
Coordinate(c)
|
|
Coordinate(d)
|
|
Coordinate(e)
|
|
Coordinate(f)
|
|
}
|
|
|
|
Close :: () {
|
|
CommandByte(#char "Z")
|
|
}
|
|
|
|
SquarePath :: (rule: u8, left: s32, top: s32, right: s32, bottom: s32) {
|
|
StartPath(rule)
|
|
MoveTo(left, top)
|
|
LineTo(right, top)
|
|
LineTo(right, bottom)
|
|
LineTo(left, bottom)
|
|
Close()
|
|
FinishPath()
|
|
}
|
|
|
|
// A circle of radius 3 about (4, 4).
|
|
CircleFile :: () {
|
|
StartFile(8, 8, 1)
|
|
StartPath(#char "N")
|
|
MoveTo(64, 16)
|
|
CurveTo(91, 16, 112, 37, 112, 64)
|
|
CurveTo(112, 91, 91, 112, 64, 112)
|
|
CurveTo(37, 112, 16, 91, 16, 64)
|
|
CurveTo(16, 37, 37, 16, 64, 16)
|
|
Close()
|
|
FinishPath()
|
|
}
|
|
|
|
Clear :: (target: VectorTarget) {
|
|
index: s32 = 0
|
|
while index < target.width * target.height {
|
|
target.coverage[index] = 0
|
|
index += 1
|
|
}
|
|
}
|
|
|
|
Difference :: (a: u8, b: u8) -> s32 {
|
|
value: s32 = cast(s32)a - cast(s32)b
|
|
if value < 0 { return -value }
|
|
return value
|
|
}
|
|
|
|
At :: (target: VectorTarget, x: s32, y: s32) -> s32 {
|
|
return cast(s32)target.coverage[y * target.width + x]
|
|
}
|
|
|
|
Area :: (target: VectorTarget) -> s32 {
|
|
total: s32 = 0
|
|
index: s32 = 0
|
|
while index < target.width * target.height {
|
|
total += cast(s32)target.coverage[index]
|
|
index += 1
|
|
}
|
|
return total / 255
|
|
}
|
|
|
|
RenderFirst :: (target: VectorTarget, scale: float32, mode: VectorMode) -> bool {
|
|
path: VectorPathView = VectorFilePath(file_bytes[:file_length], 0)
|
|
transform: VectorTransform = VectorTransform.{
|
|
.a = scale, .b = 0.0, .c = 0.0, .d = scale,
|
|
.e = 0.0, .f = 0.0
|
|
}
|
|
return VectorPathFill(path, transform, target, mode)
|
|
}
|
|
|
|
CheckSquareScales :: () -> s32 {
|
|
scale: s32 = 1
|
|
while scale <= 8 {
|
|
StartFile(8, 8, 1)
|
|
SquarePath(#char "N", 16, 16, 112, 112)
|
|
if !VectorFileValid(file_bytes[:file_length]) { return 9100 }
|
|
size: s32 = 8 * scale
|
|
target: VectorTarget = VectorTarget.{size, size, coverage[:]}
|
|
Clear(target)
|
|
factor: float32 = cast(float32)scale
|
|
if !RenderFirst(target, factor, .VectorAdd) { return 100 + scale }
|
|
edge: s32 = scale
|
|
stop: s32 = 7 * scale
|
|
y: s32 = edge
|
|
while y < stop {
|
|
x: s32 = edge
|
|
while x < stop {
|
|
if At(target, x, y) != 255 { return 200 + scale }
|
|
x += 1
|
|
}
|
|
y += 1
|
|
}
|
|
if At(target, edge - 1, 4 * scale) != 0 ||
|
|
At(target, stop, 4 * scale) != 0 { return 300 + scale }
|
|
if Area(target) != 36 * scale * scale { return 400 + scale }
|
|
scale = scale * 2
|
|
}
|
|
return 0
|
|
}
|
|
|
|
CheckCircle :: () -> s32 {
|
|
scales: [4]s32 = .{4, 8, 16, 32}
|
|
at: s32 = 0
|
|
while at < 4 {
|
|
scale: s32 = scales[at]
|
|
CircleFile()
|
|
size: s32 = 8 * scale
|
|
target: VectorTarget = VectorTarget.{size, size, coverage[:]}
|
|
Clear(target)
|
|
if !RenderFirst(target, cast(float32)scale, .VectorAdd) { return 1000 + at }
|
|
expected: float32 = 3.14159265 * 9.0 * cast(float32)(scale * scale)
|
|
actual: float32 = cast(float32)Area(target)
|
|
difference: float32 = actual - expected
|
|
if difference < 0.0 { difference = -difference }
|
|
if difference > expected * 0.01 { return 1100 + at }
|
|
at += 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
CheckHoles :: () -> s32 {
|
|
StartFile(16, 16, 1)
|
|
StartPath(#char "N")
|
|
MoveTo(16, 16)
|
|
LineTo(240, 16)
|
|
LineTo(240, 240)
|
|
LineTo(16, 240)
|
|
Close()
|
|
MoveTo(80, 80)
|
|
LineTo(176, 80)
|
|
LineTo(176, 176)
|
|
LineTo(80, 176)
|
|
Close()
|
|
FinishPath()
|
|
target: VectorTarget = VectorTarget.{16, 16, coverage[:]}
|
|
Clear(target)
|
|
if !RenderFirst(target, 1.0, .VectorAdd) { return 2000 }
|
|
if At(target, 8, 8) != 255 { return 2001 }
|
|
path: VectorPathView = VectorFilePath(file_bytes[:file_length], 0)
|
|
path.rule = #char "E"
|
|
Clear(target)
|
|
if !VectorPathFill(path, VectorTransform.{1.0, 0.0, 0.0, 1.0, 0.0, 0.0},
|
|
target, .VectorAdd) { return 2002 }
|
|
if At(target, 8, 8) != 0 || At(target, 2, 8) != 255 { return 2003 }
|
|
|
|
StartFile(16, 16, 1)
|
|
StartPath(#char "N")
|
|
MoveTo(16, 16)
|
|
LineTo(240, 16)
|
|
LineTo(240, 240)
|
|
LineTo(16, 240)
|
|
Close()
|
|
MoveTo(80, 80)
|
|
LineTo(80, 176)
|
|
LineTo(176, 176)
|
|
LineTo(176, 80)
|
|
Close()
|
|
FinishPath()
|
|
Clear(target)
|
|
if !RenderFirst(target, 1.0, .VectorAdd) { return 2004 }
|
|
if At(target, 8, 8) != 0 || At(target, 2, 8) != 255 { return 2005 }
|
|
return 0
|
|
}
|
|
|
|
CheckTransforms :: () -> s32 {
|
|
StartFile(8, 8, 1)
|
|
SquarePath(#char "N", 16, 16, 112, 112)
|
|
target: VectorTarget = VectorTarget.{32, 32, coverage[:]}
|
|
Clear(target)
|
|
if !VectorPathFill(VectorFilePath(file_bytes[:file_length], 0),
|
|
VectorTransform.{0.0, 4.0, -4.0, 0.0, 32.0, 0.0},
|
|
target, .VectorAdd) { return 3000 }
|
|
if Area(target) != 576 { return 3001 }
|
|
Clear(target)
|
|
if !VectorPathFill(VectorFilePath(file_bytes[:file_length], 0),
|
|
VectorTransform.{-4.0, 0.0, 0.0, 4.0, 32.0, 0.0},
|
|
target, .VectorAdd) { return 3002 }
|
|
if Area(target) != 576 { return 3003 }
|
|
return 0
|
|
}
|
|
|
|
CheckOutside :: () -> s32 {
|
|
StartFile(16, 16, 1)
|
|
SquarePath(#char "N", -32, 32, 32, 96)
|
|
target: VectorTarget = VectorTarget.{16, 16, coverage[:]}
|
|
Clear(target)
|
|
if !RenderFirst(target, 1.0, .VectorAdd) { return 4000 }
|
|
if Area(target) != 8 || At(target, 0, 3) != 255 || At(target, 1, 3) != 255 || At(target, 3, 3) != 0 {
|
|
return 4001
|
|
}
|
|
StartFile(16, 16, 1)
|
|
SquarePath(#char "N", 256, 256, 384, 384)
|
|
Clear(target)
|
|
if !RenderFirst(target, 1.0, .VectorAdd) { return 4002 }
|
|
if Area(target) != 0 { return 4003 }
|
|
return 0
|
|
}
|
|
|
|
// Offscreen edges on every side must not stop or disturb the rest of a path.
|
|
CheckClipped :: () -> s32 {
|
|
StartFile(8, 8, 1)
|
|
SquarePath(#char "N", -160, -160, 400, 400)
|
|
target: VectorTarget = VectorTarget.{8, 8, coverage[:]}
|
|
Clear(target)
|
|
if !RenderFirst(target, 1.0, .VectorAdd) { return 4100 }
|
|
if Area(target) != 64 { return 4101 }
|
|
return 0
|
|
}
|
|
|
|
// Compare every pixel, including curved edges and translated fractional spans.
|
|
CheckTile :: (scale: s32, failure: s32) -> s32 {
|
|
CircleFile()
|
|
full_size: s32 = 8 * scale
|
|
full: VectorTarget = VectorTarget.{full_size, full_size, coverage[:]}
|
|
Clear(full)
|
|
factor: float32 = cast(float32)scale
|
|
transform: VectorTransform = VectorTransform.{factor, 0.0, 0.0, factor, 0.1875, 0.3125}
|
|
path: VectorPathView = VectorFilePath(file_bytes[:file_length], 0)
|
|
if !VectorPathFill(path, transform, full, .VectorAdd) { return failure }
|
|
tile_size: s32 = 2 * scale
|
|
tile: VectorTarget = VectorTarget.{tile_size, tile_size, tile_coverage[:]}
|
|
origin_y: s32 = 0
|
|
while origin_y < full_size {
|
|
origin_x: s32 = 0
|
|
while origin_x < full_size {
|
|
Clear(tile)
|
|
translated: VectorTransform = transform
|
|
translated.e -= cast(float32)origin_x
|
|
translated.f -= cast(float32)origin_y
|
|
if !VectorPathFill(path, translated, tile, .VectorAdd) { return failure + 1 }
|
|
y: s32 = 0
|
|
while y < tile_size {
|
|
x: s32 = 0
|
|
while x < tile_size {
|
|
full_index: s32 = (origin_y + y) * full_size + origin_x + x
|
|
if coverage[full_index] != tile.coverage[y * tile_size + x] {
|
|
return failure + 2
|
|
}
|
|
x += 1
|
|
}
|
|
y += 1
|
|
}
|
|
origin_x += tile_size
|
|
}
|
|
origin_y += tile_size
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// Huge offscreen arcs must reduce to visible crossings rather than exhausting
|
|
// edge scratch while zoomed into an eight-pixel tile near the circle's center.
|
|
CheckLargeCurveTile :: () -> s32 {
|
|
StartFile(8, 8, 1)
|
|
StartPath(#char "N")
|
|
MoveTo(0, -1600000000)
|
|
CurveTo(880000000, -1600000000, 1600000000, -880000000, 1600000000, 0)
|
|
CurveTo(1600000000, 880000000, 880000000, 1600000000, 0, 1600000000)
|
|
CurveTo(-880000000, 1600000000, -1600000000, 880000000, -1600000000, 0)
|
|
CurveTo(-1600000000, -880000000, -880000000, -1600000000, 0, -1600000000)
|
|
Close()
|
|
FinishPath()
|
|
target: VectorTarget = VectorTarget.{8, 8, coverage[:]}
|
|
Clear(target)
|
|
if !RenderFirst(target, 8.0, .VectorAdd) { return 8300 }
|
|
if Area(target) != 64 { return 8301 }
|
|
// Finite affine coefficients can still overflow when applied to geometry.
|
|
StartFile(8, 8, 1)
|
|
SquarePath(#char "N", 0, 0, 112, 112)
|
|
Clear(target)
|
|
transform: VectorTransform = VectorTransform.{1.0, 3.0e38, 1.0e-38, 1.0, 0.0, 0.0}
|
|
if VectorPathFill(VectorFilePath(file_bytes[:file_length], 0), transform,
|
|
target, .VectorAdd) { return 8302 }
|
|
if Area(target) != 0 { return 8303 }
|
|
return 0
|
|
}
|
|
|
|
CheckModes :: () -> s32 {
|
|
StartFile(1, 1, 1)
|
|
SquarePath(#char "N", 0, 0, 16, 16)
|
|
target: VectorTarget = VectorTarget.{1, 1, coverage[:]}
|
|
coverage[0] = 100
|
|
if !RenderFirst(target, 1.0, .VectorAdd) || coverage[0] != 255 { return 5000 }
|
|
coverage[0] = 100
|
|
if !RenderFirst(target, 1.0, .VectorErase) || coverage[0] != 0 { return 5001 }
|
|
|
|
StartFile(1, 1, 1)
|
|
StartPath(#char "N")
|
|
MoveTo(0, 0)
|
|
LineTo(16, 0)
|
|
LineTo(0, 16)
|
|
FinishPath()
|
|
coverage[0] = 200
|
|
path: VectorPathView = VectorFilePath(file_bytes[:file_length], 0)
|
|
identity: VectorTransform = VectorTransform.{1.0, 0.0, 0.0, 1.0, 0.0, 0.0}
|
|
Clear(target)
|
|
if !VectorPathFill(path, identity, target, .VectorAdd) { return 5002 }
|
|
partial: u8 = coverage[0]
|
|
coverage[0] = 200
|
|
if !VectorPathFill(path, identity, target, .VectorErase) { return 5003 }
|
|
erased: u8 = coverage[0]
|
|
expected_erase: s32 = (200 * (255 - cast(s32)partial) + 127) / 255
|
|
if cast(s32)erased != expected_erase { return 5004 }
|
|
if !VectorPathFill(path, identity, target, .VectorAdd) { return 5005 }
|
|
expected: s32 = (255 * cast(s32)erased + (255 - cast(s32)erased) *
|
|
cast(s32)partial + 127) / 255
|
|
if cast(s32)coverage[0] != expected { return 5006 }
|
|
return 0
|
|
}
|
|
|
|
CheckStrokes :: () -> s32 {
|
|
target: VectorTarget = VectorTarget.{32, 32, coverage[:]}
|
|
Clear(target)
|
|
line: [2]Vector2 = .{Vector2.{4.0, 16.0}, Vector2.{28.0, 16.0}}
|
|
identity: VectorTransform = VectorTransform.{1.0, 0.0, 0.0, 1.0, 0.0, 0.0}
|
|
if !VectorPolylineStroke(line[:], 4.0, identity, target, .VectorAdd) {
|
|
return 6000
|
|
}
|
|
area: s32 = Area(target)
|
|
if area < 96 || area > 122 { return 6001 }
|
|
Clear(target)
|
|
dot: [1]Vector2 = .{Vector2.{16.0, 16.0}}
|
|
if !VectorPolylineStroke(dot[:], 4.0, identity, target, .VectorAdd) {
|
|
return 6002
|
|
}
|
|
dot_area: s32 = Area(target)
|
|
if dot_area < 7 || dot_area > 18 { return 6003 }
|
|
|
|
Clear(target)
|
|
straight: [2]Vector2 = .{Vector2.{4.0, 16.0}, Vector2.{28.0, 16.0}}
|
|
if !VectorPolylineStroke(straight[:], 4.0, identity, target, .VectorAdd) {
|
|
return 6004
|
|
}
|
|
copy: [128 * 128]u8
|
|
index: s32 = 0
|
|
while index < 32 * 32 {
|
|
copy[index] = coverage[index]
|
|
index += 1
|
|
}
|
|
Clear(target)
|
|
collinear: [3]Vector2 = .{
|
|
Vector2.{4.0, 16.0},
|
|
Vector2.{16.0, 16.0},
|
|
Vector2.{28.0, 16.0}
|
|
}
|
|
if !VectorPolylineStroke(collinear[:], 4.0, identity, target, .VectorAdd) {
|
|
return 6005
|
|
}
|
|
index = 0
|
|
while index < 32 * 32 {
|
|
if coverage[index] != copy[index] { return 6006 }
|
|
index += 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
CheckMalformed :: () -> s32 {
|
|
wrong: [4]u8 = .{#char "X", #char "V", #char "P", #char "1"}
|
|
if VectorFileValid(wrong[:]) { return 7000 }
|
|
StartFile(1, 1, 1)
|
|
StartPath(#char "N")
|
|
MoveTo(16, 16)
|
|
file_length -= 1
|
|
Raw(cast(u8)128)
|
|
if VectorFileValid(file_bytes[:file_length]) { return 7001 }
|
|
|
|
StartFile(1, 1, 1)
|
|
Raw(#char "N")
|
|
Varint(10)
|
|
CommandByte(#char "M")
|
|
Coordinate(16)
|
|
Coordinate(16)
|
|
if VectorFileValid(file_bytes[:file_length]) { return 7002 }
|
|
|
|
StartFile(1, 1, 1)
|
|
StartPath(#char "N")
|
|
MoveTo(16, 16)
|
|
CommandByte(#char "X")
|
|
FinishPath()
|
|
if VectorFileValid(file_bytes[:file_length]) { return 7003 }
|
|
StartFile(1, 1, 1)
|
|
SquarePath(#char "N", 0, 0, 16, 16)
|
|
return 0
|
|
}
|
|
|
|
VectorPathCases :: () -> s32 {
|
|
result: s32 = CheckSquareScales()
|
|
if result != 0 { return result }
|
|
result = CheckCircle()
|
|
if result != 0 { return result }
|
|
result = CheckHoles()
|
|
if result != 0 { return result }
|
|
result = CheckTransforms()
|
|
if result != 0 { return result }
|
|
result = CheckOutside()
|
|
if result != 0 { return result }
|
|
result = CheckClipped()
|
|
if result != 0 { return result }
|
|
result = CheckTile(1, 8000)
|
|
if result != 0 { return result }
|
|
result = CheckTile(3, 8100)
|
|
if result != 0 { return result }
|
|
result = CheckTile(8, 8200)
|
|
if result != 0 { return result }
|
|
result = CheckLargeCurveTile()
|
|
if result != 0 { return result }
|
|
result = CheckModes()
|
|
if result != 0 { return result }
|
|
result = CheckStrokes()
|
|
if result != 0 { return result }
|
|
result = CheckMalformed()
|
|
if result != 0 { return result }
|
|
if !VectorFileValid(file_bytes[:file_length]) { return 9000 }
|
|
if VectorFileSize(file_bytes[:file_length]).x != 1.0 { return 9001 }
|
|
if VectorFilePathCount(file_bytes[:file_length]) != 1 { return 9002 }
|
|
return 0
|
|
}
|