Skip to content

Commit

Permalink
Actually add extras/iterators tests
Browse files Browse the repository at this point in the history
Mentioned in 7fa1aea
  • Loading branch information
carlobaldassi committed Oct 25, 2012
1 parent 14f638f commit 3a0e570
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require("../extras/iterators.jl")
import Iterators.*

# aux function emulating a comprehension [x for x in f]
function buildvec(f)

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Oct 26, 2012

Member

I think we should add this the standard library and call it collect.

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson Oct 26, 2012

Member

Or you could write [f...]

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Oct 26, 2012

Member

Ah! I hadn't realized that would work. Very useful idiom.

This comment has been minimized.

Copy link
@carlobaldassi

carlobaldassi Oct 26, 2012

Author Member

Doesn't really work, as [f...] expansion needs to know the length of the result:

julia> [take(count(), 5)...]
no method length(Take,)
 in method_missing at base.jl:70
 in append_any at base.jl:123

Playing around, I also found a problem with UTF Strings:

julia> ["xyz"...]
3-element Char Array:
 'x'
 'y'
 'z'

julia> ["ẍÿƶ"...]
Segmentation fault (core dumped)

I'll open an issue.

v = Any[]
for i = f
push(v, i)
end
return v
end

macro assert_buildvec(ex...)
:(@assert buildvec($(ex[1])) == $(ex[2]))
end

@assert_buildvec take(count(), 0) []
@assert_buildvec take(count(), 5) [0:4]
@assert_buildvec take(count(1,3), 5) [1:3:13]
@assert_buildvec take(count(zeros(2,2)), 3) [i * eye(2) for i=0:2]

@assert_buildvec drop(1:5, 5) []
@assert_buildvec drop(1:0, 0) []
@assert_buildvec drop(1:5, 2) [3:5]

@assert_buildvec cycle(1:0) []
@assert_buildvec take(cycle(1:3), 8) [1:3,1:3,1:2]

@assert_buildvec repeat('a', 0) []
@assert_buildvec repeat('a', 5) ['a' for i = 1:5]
@assert_buildvec take(repeat('a'), 5) ['a' for i = 1:5]

@assert_buildvec chain() []
@assert_buildvec chain(1:0) []
@assert_buildvec chain(4:8, 1:1) [4:8, 1:1]
@assert_buildvec chain(1:1, 4:8) [1:1, 4:8]
@assert_buildvec chain(1:3, 2:4, 1:0) [1:3, 2:4, 1:0]

# matrix to vector
m2v(m) = reshape(m, length(m))

@assert_buildvec product() []
@assert_buildvec product(1:2,1:0,1:1) []
@assert_buildvec product(1:2,1:1) m2v([(i,j) for i=1:2, j=1:1])
@assert_buildvec product(1:1,1:3) m2v([(i,j) for i=1:1, j=1:3])
@assert_buildvec product(2:4,1:3,4:8) m2v([(i,j,k) for i=2:4, j=1:3, k=4:8])

0 comments on commit 3a0e570

Please sign in to comment.