dymokomi/luce-vector
Source of release 0.6.0
0.6.0 / src / graph / tests.lucb
#==============================================================================================
#
# graph/tests - Every node, clones, seeds and the cache
#
#==============================================================================================
## A graph grown one edit at a time, as the editor grows it.
struct TestGraph:
var graph: Graph
func node(kind: i64) -> u32!:
var made: u32 = 0
try replace_graph(&self.graph, try graph_with_node(&self.graph, kind, 0.0, 0.0, &made))
return made
func link(source: u32, target: u32, port: u32) -> !:
try replace_graph(&self.graph, try graph_with_link(&self.graph, source, target, port))
func set(id: u32, slot: usize, value: f64) -> !:
try replace_graph(&self.graph, try graph_with_value(&self.graph, id, slot, value))
func close():
free_graph(&self.graph)
## `graph` evaluated afresh (no cache kept) into `out`.
func evaluated(graph: const Graph*, elements: const VectorElement[], out: ShapeList*) -> !:
var cache: GraphCache
defer cache.release()
out.release()
_ = try evaluate_graph(graph, elements, &cache, out)
## Where a shape's own origin lands: its matrix's translation.
func origin_of(spec: ShapeSpec) -> f64[2]:
return [spec.matrix[4], spec.matrix[5]]
func close_to(a: f64, b: f64) -> bool:
return math.abs(a - b) < 1e-6
test "each shape generator makes one shape of its kind, placed and painted by its values":
var built = TestGraph()
defer built.close()
let output = try built.node(node_output)
var none_here: VectorElement[]
var shapes: ShapeList
defer shapes.release()
for kind in [node_rectangle, node_ellipse, node_polygon, node_star]:
let shape = try built.node(kind)
try built.set(shape, slot_x, 100.0)
try built.set(shape, slot_y, 50.0)
try built.set(shape, slot_width, 40.0)
try built.set(shape, slot_fill, 0.25)
try built.link(shape, output, 0)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 1)
let spec = shapes.specs[0]
assert(close_to(origin_of(spec)[0], 100.0) and close_to(origin_of(spec)[1], 50.0) and close_to(spec.fill_red, 0.25) and spec.filled and not spec.stroked)
if kind == node_rectangle:
assert(spec.kind == 0 and close_to(spec.width, 40.0) and close_to(spec.x, -20.0))
elif kind == node_ellipse:
assert(spec.kind == 1)
elif kind == node_polygon:
assert(spec.kind == 3 and spec.sides == 6)
else:
# Five tips and five inner corners, closed.
assert(spec.kind == 4 and spec.closed and spec.path.length == 10 * anchor_values)
# A value past its range is held to it, and a count is whole.
let polygon = try built.node(node_polygon)
try built.set(polygon, slot_special, 200.7)
let at = built.graph.slot_of(polygon) else trap("added")
assert(built.graph.nodes[at].values[slot_special] == 64.0)
test "point generators and Clone put a copy of every shape on every point":
var built = TestGraph()
defer built.close()
var none_here: VectorElement[]
var shapes: ShapeList
defer shapes.release()
let output = try built.node(node_output)
let star = try built.node(node_star)
let square = try built.node(node_rectangle)
let both = try built.node(node_merge)
try built.link(star, both, 0)
try built.link(square, both, 1)
let clone = try built.node(node_clone)
try built.link(both, clone, 0)
try built.link(clone, output, 0)
# A grid of 3 by 2: six points, two shapes on each.
let grid = try built.node(node_grid)
try built.set(grid, 0, 3.0)
try built.set(grid, 1, 2.0)
try built.set(grid, 2, 100.0)
try built.set(grid, 3, 50.0)
try built.link(grid, clone, 1)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 12)
assert(close_to(origin_of(shapes.specs[0])[0], -100.0) and close_to(origin_of(shapes.specs[0])[1], -25.0) and shapes.specs[0].kind == 4 and shapes.specs[1].kind == 0)
assert(close_to(origin_of(shapes.specs[11])[0], 100.0) and close_to(origin_of(shapes.specs[11])[1], 25.0))
# A circle of 12, each copy turned to its point's angle: the one at the right
# stands a quarter turn round.
let circle = try built.node(node_circle)
try built.set(circle, 0, 12.0)
try built.set(circle, 1, 300.0)
try built.set(circle, 3, 500.0)
try built.set(circle, 4, 400.0)
try built.link(circle, clone, 1)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 24)
assert(close_to(origin_of(shapes.specs[0])[0], 500.0) and close_to(origin_of(shapes.specs[0])[1], 100.0))
let right = shapes.specs[6]
assert(close_to(origin_of(right)[0], 800.0) and close_to(origin_of(right)[1], 400.0) and close_to(right.matrix[0], 0.0) and close_to(right.matrix[1], 1.0))
# Not turned, every copy stands upright.
try built.set(clone, 0, 0.0)
try evaluated(&built.graph, none_here, &shapes)
assert(close_to(shapes.specs[6].matrix[0], 1.0) and close_to(shapes.specs[6].matrix[1], 0.0))
# A line of 5 from (0, 0) to (400, 0): evenly, ends included.
let line = try built.node(node_line)
try built.set(line, 0, 5.0)
try built.link(line, clone, 1)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 10 and close_to(origin_of(shapes.specs[2])[0], 100.0) and close_to(origin_of(shapes.specs[8])[0], 400.0))
# A Transform after the Clone moves them all, and turns them about their middle.
let move = try built.node(node_transform)
try built.set(move, 0, 30.0)
try built.set(move, 1, -20.0)
try built.link(clone, move, 0)
try built.link(move, output, 0)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 10 and close_to(origin_of(shapes.specs[2])[0], 130.0) and close_to(origin_of(shapes.specs[2])[1], -20.0))
test "scatter, points of a shape and the Number, Random and Gradient fields":
var built = TestGraph()
defer built.close()
var none_here: VectorElement[]
var shapes: ShapeList
defer shapes.release()
let output = try built.node(node_output)
let dot = try built.node(node_ellipse)
try built.set(dot, slot_width, 4.0)
try built.set(dot, slot_height, 4.0)
let clone = try built.node(node_clone)
try built.link(dot, clone, 0)
try built.link(clone, output, 0)
# Scattered in its box: all inside it.
let scatter = try built.node(node_scatter)
try built.set(scatter, 0, 200.0)
try built.set(scatter, 2, 1000.0)
try built.set(scatter, 3, 2000.0)
try built.set(scatter, 4, 300.0)
try built.set(scatter, 5, 100.0)
try built.link(scatter, clone, 1)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 200)
for spec in shapes.view():
let at = origin_of(spec)
assert(at[0] >= 1000.0 and at[0] <= 1300.0 and at[1] >= 2000.0 and at[1] <= 2100.0)
# Inside a circle of radius 100 at the origin: every point within it.
let disc = try built.node(node_ellipse)
try built.set(disc, slot_width, 200.0)
try built.set(disc, slot_height, 200.0)
try built.link(disc, scatter, 0)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 200)
for spec in shapes.view():
assert(math.hypot(origin_of(spec)[0], origin_of(spec)[1]) <= 100.5)
# A hexagon's corners, then 10 points along its outline.
let hexagon = try built.node(node_polygon)
let corners = try built.node(node_outline)
try built.link(hexagon, corners, 0)
try built.link(corners, clone, 1)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 6 and close_to(origin_of(shapes.specs[0])[0], 0.0) and close_to(origin_of(shapes.specs[0])[1], -80.0))
try built.set(corners, 0, 1.0)
try built.set(corners, 1, 10.0)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 10)
# A Number wired to the count drives it.
let number = try built.node(node_number)
try built.set(number, 0, 7.0)
try built.link(number, corners, 1)
try evaluated(&built.graph, none_here, &shapes)
assert(shapes.count == 7)
# A Random scale varies copy to copy, within its range; a Gradient colors
# the first copy its start and the last its end.
let random = try built.node(node_random)
try built.link(random, clone, 2)
let gradient = try built.node(node_gradient)
try built.link(gradient, clone, 4)
try evaluated(&built.graph, none_here, &shapes)
var smallest = 1e9
var largest = 0.0
for spec in shapes.view():
let scale = math.hypot(spec.matrix[0], spec.matrix[1])
smallest = math.min(smallest, scale)
largest = math.max(largest, scale)
assert(smallest >= 0.5 - 1e-9 and largest <= 1.5 + 1e-9 and largest - smallest > 0.05)
assert(close_to(shapes.specs[0].fill_red, 0.8069) and close_to(shapes.specs[6].fill_blue, 0.8))
# A wire of the wrong type, or round a loop, is refused.
let refused = graph_with_link(&built.graph, number, clone, 0) catch _:
recover Graph()
assert(not refused.present())
let looped = graph_with_link(&built.graph, clone, dot, 0) catch _:
recover Graph()
assert(not looped.present())
test "a graph is the same every time, a seed changes it, and an edit computes only what is downstream":
var built = TestGraph()
defer built.close()
var none_here: VectorElement[]
let output = try built.node(node_output)
let star = try built.node(node_star)
let clone = try built.node(node_clone)
let scatter = try built.node(node_scatter)
let circle = try built.node(node_circle)
let both = try built.node(node_merge)
try built.set(clone, 2, 45.0)
try built.set(clone, 3, 0.5)
try built.set(clone, 4, 0.3)
try built.link(star, clone, 0)
try built.link(scatter, clone, 1)
try built.link(clone, both, 0)
let ring = try built.node(node_clone)
try built.link(star, ring, 0)
try built.link(circle, ring, 1)
try built.link(ring, both, 1)
try built.link(both, output, 0)
var first: ShapeList
defer first.release()
var second: ShapeList
defer second.release()
try evaluated(&built.graph, none_here, &first)
try evaluated(&built.graph, none_here, &second)
assert(first.count == 112 and second.count == 112)
for at in 0..<first.count:
assert(same_spec(&first.specs[at], &second.specs[at]))
try built.set(clone, 5, 2.0)
try evaluated(&built.graph, none_here, &second)
var differ: usize = 0
for at in 0..<first.count:
if not same_spec(&first.specs[at], &second.specs[at]):
differ += 1
# The jittered copies move; the ring's twelve do not.
assert(differ == 100)
# With a kept cache: all six at first, none again, then the circle, its
# Clone and the Merge after an edit to the circle.
var cache: GraphCache
defer cache.release()
var out: ShapeList
defer out.release()
assert((try evaluate_graph(&built.graph, none_here, &cache, &out)) == 6)
out.release()
assert((try evaluate_graph(&built.graph, none_here, &cache, &out)) == 0)
out.release()
try built.set(circle, 1, 90.0)
assert((try evaluate_graph(&built.graph, none_here, &cache, &out)) == 3)
assert(out.count == 112)