-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[master] changes to algorithm to support move-only types (#43)
- Loading branch information
Showing
4 changed files
with
206 additions
and
21 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/perl | ||
|
||
use warnings; | ||
use strict; | ||
$ENV{"DALE_TEST_ARGS"} ||= ""; | ||
my $test_dir = $ENV{"DALE_TEST_DIR"} || "."; | ||
$ENV{PATH} .= ":."; | ||
|
||
use Data::Dumper; | ||
use Test::More tests => 3; | ||
|
||
my @res = `dalec $ENV{"DALE_TEST_ARGS"} -lm $test_dir/t/src/algorithm-rv.dt -o algorithm-rv `; | ||
is_deeply(\@res, [], 'No compilation errors'); | ||
@res = `./algorithm-rv`; | ||
is($?, 0, 'Program executed successfully'); | ||
|
||
chomp for @res; | ||
|
||
is_deeply(\@res, [ | ||
'Did not find pointer', | ||
'Found pointer', | ||
'Did not find lower bound for 4', | ||
'Found lower bound for 3 (3)', | ||
'Did not find upper bound for 3', | ||
'Found upper bound for 2 (3)', | ||
'Found 3', | ||
'Did not find 4', | ||
'Lower bound is 2', | ||
'Upper bound is 3', | ||
], | ||
'Got correct results'); | ||
|
||
`rm algorithm-rv`; | ||
|
||
1; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
(import cstdio) | ||
(import unistd) | ||
(import macros) | ||
(import unique-ptr) | ||
(import vector) | ||
(import derivations) | ||
(import algorithms) | ||
|
||
(std.concepts.instantiate UniquePtr int) | ||
|
||
(def = (fn intern bool ((a (const (p int))) (b (const (p int)))) | ||
(= (@ a) (@ b)))) | ||
(def < (fn intern bool ((a (const (p int))) (b (const (p int)))) | ||
(< (@ a) (@ b)))) | ||
(def = (fn extern bool ((a (ref (const (UniquePtr int)))) | ||
(b (ref (const (UniquePtr int))))) | ||
(= (core-@:@ a pointer) (core-@:@ b pointer)))) | ||
(def < (fn extern bool ((a (ref (const (UniquePtr int)))) | ||
(b (ref (const (UniquePtr int))))) | ||
(< (core-@:@ a pointer) (core-@:@ b pointer)))) | ||
|
||
(std.concepts.instantiate relations (UniquePtr int)) | ||
(std.concepts.instantiate swap (UniquePtr int)) | ||
(std.concepts.implement Swappable (UniquePtr int)) | ||
(std.concepts.implement EqualityComparable (UniquePtr int)) | ||
(std.concepts.implement LessThanComparable (UniquePtr int)) | ||
|
||
(std.concepts.instantiate Vector (UniquePtr int)) | ||
(std.concepts.instantiate find (Iterator (Vector (UniquePtr int)))) | ||
(std.concepts.instantiate lower-bound (Iterator (Vector (UniquePtr int)))) | ||
(std.concepts.instantiate upper-bound (Iterator (Vector (UniquePtr int)))) | ||
(std.concepts.instantiate binary-search (Iterator (Vector (UniquePtr int)))) | ||
(std.concepts.instantiate equal-range (Iterator (Vector (UniquePtr int)))) | ||
|
||
(def main | ||
(fn extern-c int (void) | ||
(let ((vec (Vector (UniquePtr int)) (init vec 1)) | ||
(myint1 \ (malloc' 1 int)) | ||
(myptr1 (UniquePtr int)) | ||
(myint2 \ (malloc' 1 int)) | ||
(myptr2 (UniquePtr int)) | ||
(myint22 \ (malloc' 1 int)) | ||
(myptr22 (UniquePtr int)) | ||
(myint3 \ (malloc' 1 int)) | ||
(myptr3 (UniquePtr int)) | ||
(myint32 \ (malloc' 1 int)) | ||
(myptr32 (UniquePtr int)) | ||
(myint4 \ (malloc' 1 int)) | ||
(myptr4 (UniquePtr int))) | ||
(setf myint1 1) | ||
(init myptr1 myint1) | ||
(setf myint2 2) | ||
(init myptr2 myint2) | ||
(setf myint22 2) | ||
(init myptr22 myint2) | ||
(setf myint3 3) | ||
(init myptr3 myint3) | ||
(setf myint32 3) | ||
(init myptr32 myint32) | ||
(setf myint4 4) | ||
(init myptr4 myint4) | ||
|
||
(push-back vec (move myptr1)) | ||
(push-back vec (move myptr2)) | ||
(push-back vec (move myptr3)) | ||
|
||
; find. | ||
(let ((b \ (begin vec)) | ||
(e \ (end vec)) | ||
(f (Iterator (Vector (UniquePtr int))))) | ||
(setv f (find b e myptr4)) | ||
(if (not (= f e)) | ||
(printf "Found pointer\n") | ||
(printf "Did not find pointer\n")) | ||
(setv f (find b e myptr32)) | ||
(if (not (= f e)) | ||
(printf "Found pointer\n") | ||
(printf "Did not find pointer\n"))) | ||
|
||
; lower-bound. | ||
(let ((b \ (begin vec)) | ||
(e \ (end vec)) | ||
(f (Iterator (Vector (UniquePtr int))))) | ||
(setv f (lower-bound b e myptr4)) | ||
(if (not (= f e)) | ||
(printf "Found lower bound for 4\n") | ||
(printf "Did not find lower bound for 4\n")) | ||
(setv f (lower-bound b e myptr32)) | ||
(if (not (= f e)) | ||
(printf "Found lower bound for 3 (%d)\n" | ||
(@ (get (@ (source f))))) | ||
(printf "Did not find lower bound for 3\n"))) | ||
|
||
; upper-bound. | ||
(let ((b \ (begin vec)) | ||
(e \ (end vec)) | ||
(f (Iterator (Vector (UniquePtr int))))) | ||
(setv f (upper-bound b e myptr32)) | ||
(if (not (= f e)) | ||
(printf "Found upper bound for 3\n") | ||
(printf "Did not find upper bound for 3\n")) | ||
(setv f (upper-bound b e myptr22)) | ||
(if (not (= f e)) | ||
(printf "Found upper bound for 2 (%d)\n" | ||
(@ (get (@ (source f))))) | ||
(printf "Did not find upper bound for 2\n"))) | ||
|
||
; binary-search. | ||
(let ((b \ (begin vec)) | ||
(e \ (end vec)) | ||
(f bool)) | ||
(setv f (binary-search b e myptr32)) | ||
(if f | ||
(printf "Found 3\n") | ||
(printf "Did not find 3\n")) | ||
(setv f (binary-search b e myptr4)) | ||
(if f | ||
(printf "Found 4\n") | ||
(printf "Did not find 4\n"))) | ||
|
||
; equal-range. | ||
(let ((b \ (begin vec)) | ||
(e \ (end vec)) | ||
(f (Pair (Iterator (Vector (UniquePtr int))) | ||
(Iterator (Vector (UniquePtr int)))))) | ||
(setv f (equal-range b e myptr22)) | ||
(printf "Lower bound is %d\n" | ||
(@ (get (@ (source (@: f first)))))) | ||
(printf "Upper bound is %d\n" | ||
(@ (get (@ (source (@: f second))))))) | ||
|
||
0))) |