-
-
Save ab5tract/ef4190612d84f49decc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
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