Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ab5tract/73bf6ca1663073b33e71 to your computer and use it in GitHub Desktop.
Save ab5tract/73bf6ca1663073b33e71 to your computer and use it in GitHub Desktop.
Overlaps With
use Test;
sub overlaps-with( $r1, $r2 ) {
$r1.min ~~ $r2 and $r1.max ~~ $r2 or $r2.min ~~ $r1 and $r2.max ~~ $r1
or ( $r1.min == $r2.min and ($r1.max <= $r2.max or $r2.max <= $r1.max) )
or ( $r2.min < $r1.min < $r2.max <= $r1.max )
or ( $r1.min < $r2.min < $r1.max <= $r2.max );
}
my @range-tests = (
[ $(0..6), $(6^..12), False ],
[ $(12^..^23), $(23..44), False ],
[ $(23..44), $(42..64), True ],
[ $(6^..^7), $(6^..^8), True ],
[ $(6^..^7), $(7^..^8), False ],
[ $(-6^..^7), $(-7^..^8), True ],
[ $(-6^..^7), $(-7..-6), False ],
[ $(3^..^7), $(6^..^8), True ],
[ $(3^..^20), $(6^..^8), True ],
[ $(3^..^20), $(6^..^8), True ],
[ $(6^..^8), $(3^..^20), True ],
[ $(6..2), $(2..5), False ],
[ $(0..0), $(0..0), True ],
[ $(-10..10), $(0..0), True ],
[ $(0..2), $(0^..^2), True ],
[ $(5^..^6), $(5.5^..^5.6), True ],
[ $(5^..^6), $(5.5^..6), True ],
[ $(5.5..6), $(5..6), True ],
[ $(5.5^..^5.6), $(6..7), False ],
);
for @range-tests -> [ $r1, $r2, $outcome ] {
is overlaps-with( $r1, $r2 ), $outcome, "$r1.gist() overlaps $r2.gist()";
is overlaps-with( $r2, $r1 ), $outcome, "$r2.gist() overlaps $r1.gist()";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment