Skip to content

Commit 61f82e4

Browse files
authored
Add unit tests for tiling (#881)
It would be nice to also add some tests for how it behaves at tile boundaries, but since at least from my experiments we do not seem to produce ideal results for those cases (yet?), I've left them out for now.
1 parent bf95b85 commit 61f82e4

File tree

1 file changed

+208
-1
lines changed
  • sparse_strips/vello_common/src

1 file changed

+208
-1
lines changed

sparse_strips/vello_common/src/tile.rs

Lines changed: 208 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,214 @@ impl Tiles {
322322
#[cfg(test)]
323323
mod tests {
324324
use crate::flatten::{Line, Point};
325-
use crate::tile::Tiles;
325+
use crate::tile::{Tile, Tiles};
326+
327+
#[test]
328+
fn cull_line_at_top() {
329+
let line = Line {
330+
p0: Point { x: 3.0, y: -5.0 },
331+
p1: Point { x: 9.0, y: -1.0 },
332+
};
333+
334+
let mut tiles = Tiles::new();
335+
tiles.make_tiles(&[line], 100, 100);
336+
337+
assert!(tiles.is_empty());
338+
}
339+
340+
#[test]
341+
fn cull_line_at_right() {
342+
let line = Line {
343+
p0: Point { x: 101.0, y: 0.0 },
344+
p1: Point { x: 103.0, y: 20.0 },
345+
};
346+
347+
let mut tiles = Tiles::new();
348+
tiles.make_tiles(&[line], 100, 100);
349+
350+
assert!(tiles.is_empty());
351+
}
352+
353+
#[test]
354+
fn cull_line_at_bottom() {
355+
let line = Line {
356+
p0: Point { x: 30.0, y: 101.0 },
357+
p1: Point { x: 35.0, y: 105.0 },
358+
};
359+
360+
let mut tiles = Tiles::new();
361+
tiles.make_tiles(&[line], 100, 100);
362+
363+
assert!(tiles.is_empty());
364+
}
365+
366+
#[test]
367+
fn partially_cull_line_exceeding_viewport() {
368+
let line = Line {
369+
p0: Point { x: -2.0, y: -3.0 },
370+
p1: Point { x: 2.0, y: 1.0 },
371+
};
372+
373+
let mut tiles = Tiles::new();
374+
tiles.make_tiles(&[line], 100, 100);
375+
376+
assert_eq!(tiles.tile_buf, [Tile::new(0, 0, 0, true)]);
377+
}
378+
379+
#[test]
380+
fn horizontal_straight_line() {
381+
let line = Line {
382+
p0: Point { x: 1.5, y: 1.0 },
383+
p1: Point { x: 8.5, y: 1.0 },
384+
};
385+
386+
let mut tiles = Tiles::new();
387+
tiles.make_tiles(&[line], 100, 100);
388+
tiles.sort_tiles();
389+
390+
assert_eq!(
391+
tiles.tile_buf,
392+
[
393+
Tile::new(0, 0, 0, false),
394+
Tile::new(1, 0, 0, false),
395+
Tile::new(2, 0, 0, false),
396+
]
397+
);
398+
}
399+
400+
#[test]
401+
fn vertical_straight_line() {
402+
let line = Line {
403+
p0: Point { x: 1.0, y: 1.5 },
404+
p1: Point { x: 1.0, y: 8.5 },
405+
};
406+
407+
let mut tiles = Tiles::new();
408+
tiles.make_tiles(&[line], 100, 100);
409+
tiles.sort_tiles();
410+
411+
assert_eq!(
412+
tiles.tile_buf,
413+
[
414+
Tile::new(0, 0, 0, false),
415+
Tile::new(0, 1, 0, true),
416+
Tile::new(0, 2, 0, true),
417+
]
418+
);
419+
}
420+
421+
#[test]
422+
fn top_left_to_bottom_right() {
423+
let line = Line {
424+
p0: Point { x: 1.0, y: 1.0 },
425+
p1: Point { x: 11.0, y: 8.5 },
426+
};
427+
428+
let mut tiles = Tiles::new();
429+
tiles.make_tiles(&[line], 100, 100);
430+
tiles.sort_tiles();
431+
432+
assert_eq!(
433+
tiles.tile_buf,
434+
[
435+
Tile::new(0, 0, 0, false),
436+
Tile::new(1, 0, 0, false),
437+
Tile::new(1, 1, 0, true),
438+
Tile::new(2, 1, 0, false),
439+
Tile::new(2, 2, 0, true),
440+
]
441+
);
442+
}
443+
444+
#[test]
445+
fn bottom_right_to_top_left() {
446+
let line = Line {
447+
p0: Point { x: 11.0, y: 8.5 },
448+
p1: Point { x: 1.0, y: 1.0 },
449+
};
450+
451+
let mut tiles = Tiles::new();
452+
tiles.make_tiles(&[line], 100, 100);
453+
tiles.sort_tiles();
454+
455+
assert_eq!(
456+
tiles.tile_buf,
457+
[
458+
Tile::new(0, 0, 0, false),
459+
Tile::new(1, 0, 0, false),
460+
Tile::new(1, 1, 0, true),
461+
Tile::new(2, 1, 0, false),
462+
Tile::new(2, 2, 0, true),
463+
]
464+
);
465+
}
466+
467+
#[test]
468+
fn bottom_left_to_top_right() {
469+
let line = Line {
470+
p0: Point { x: 2.0, y: 11.0 },
471+
p1: Point { x: 14.0, y: 6.0 },
472+
};
473+
474+
let mut tiles = Tiles::new();
475+
tiles.make_tiles(&[line], 100, 100);
476+
tiles.sort_tiles();
477+
478+
assert_eq!(
479+
tiles.tile_buf,
480+
[
481+
Tile::new(2, 1, 0, false),
482+
Tile::new(3, 1, 0, false),
483+
Tile::new(0, 2, 0, false),
484+
Tile::new(1, 2, 0, false),
485+
Tile::new(2, 2, 0, true),
486+
]
487+
);
488+
}
489+
490+
#[test]
491+
fn top_right_to_bottom_left() {
492+
let line = Line {
493+
p0: Point { x: 14.0, y: 6.0 },
494+
p1: Point { x: 2.0, y: 11.0 },
495+
};
496+
497+
let mut tiles = Tiles::new();
498+
tiles.make_tiles(&[line], 100, 100);
499+
tiles.sort_tiles();
500+
501+
assert_eq!(
502+
tiles.tile_buf,
503+
[
504+
Tile::new(2, 1, 0, false),
505+
Tile::new(3, 1, 0, false),
506+
Tile::new(0, 2, 0, false),
507+
Tile::new(1, 2, 0, false),
508+
Tile::new(2, 2, 0, true),
509+
]
510+
);
511+
}
512+
513+
#[test]
514+
fn two_lines_in_single_tile() {
515+
let line_1 = Line {
516+
p0: Point { x: 1.0, y: 3.0 },
517+
p1: Point { x: 3.0, y: 3.0 },
518+
};
519+
520+
let line_2 = Line {
521+
p0: Point { x: 3.0, y: 3.0 },
522+
p1: Point { x: 0.0, y: 1.0 },
523+
};
524+
525+
let mut tiles = Tiles::new();
526+
tiles.make_tiles(&[line_1, line_2], 100, 100);
527+
528+
assert_eq!(
529+
tiles.tile_buf,
530+
[Tile::new(0, 0, 0, false), Tile::new(0, 0, 1, false),]
531+
);
532+
}
326533

327534
#[test]
328535
// See https://github.com/LaurenzV/cpu-sparse-experiments/issues/46.

0 commit comments

Comments
 (0)