diff --git a/examples/hpl.jl b/examples/hpl.jl index ad7bb9408e811..fbcbae00d1f8f 100644 --- a/examples/hpl.jl +++ b/examples/hpl.jl @@ -58,7 +58,7 @@ function panel_factor_seq(A, I, col_dep) ## Factorize a panel K = I[1]:n - (A[K,I], panel_p) = lu!(A[K,I]) # Economy mode + panel_p = lufact!(sub(A, K, I))[:p] # Economy mode ## Panel permutation panel_p = K[panel_p] @@ -197,7 +197,7 @@ function panel_factor_par(A_KI, col_dep) @assert col_dep ## Factorize a panel - (A_KI, panel_p) = lu!(A_KI) # Economy mode + panel_p = lufact!(A_KI)[:p] # Economy mode return (A_KI, panel_p) diff --git a/examples/lru.jl b/examples/lru.jl index 09e42676c2e9b..51007519c6098 100644 --- a/examples/lru.jl +++ b/examples/lru.jl @@ -1,3 +1,4 @@ +module LRUExample # An LRU (Least Recently Used) cache is an associative data structure which # maintains its contents in an order such that the most recently used item # is at the beginning of the structure, and the least recently used at the end. @@ -124,3 +125,5 @@ function delete!(lru::LRU, key) delete!(lru.ht, key) delete!(lru.q, idx) end + +end # module \ No newline at end of file diff --git a/examples/lru_test.jl b/examples/lru_test.jl index 573e3338bb30e..87bc59bd65f4a 100644 --- a/examples/lru_test.jl +++ b/examples/lru_test.jl @@ -1,50 +1,45 @@ -require("lru") +using LRUExample -const TestLRU = UnboundedLRU{ASCIIString, ASCIIString}() -const TestBLRUs = BoundedLRU{ASCIIString, ASCIIString}(100) -const TestBLRUm = BoundedLRU{ASCIIString, ASCIIString}(1000) -const TestBLRUl = BoundedLRU{ASCIIString, ASCIIString}(10000) -const TestBLRUxl = BoundedLRU{ASCIIString, ASCIIString}(100000) +TestLRU = LRUExample.UnboundedLRU{ASCIIString, ASCIIString}() +TestBLRU = LRUExample.BoundedLRU{ASCIIString, ASCIIString}(1000) get_str(i) = ascii(vcat(map(x->[x>>4; x&0x0F], reinterpret(Uint8, [int32(i)]))...)) -isbounded{L<:LRU}(::Type{L}) = any(map(n->n==:maxsize, L.names)) -isbounded{L<:LRU}(l::L) = isbounded(L) +isbounded{L<:LRUExample.LRU}(::Type{L}) = any(map(n->n==:maxsize, L.names)) +isbounded{L<:LRUExample.LRU}(l::L) = isbounded(L) nmax = int(logspace(2, 5, 4)) -println("LRU consistency tests") -for lru in ( - TestLRU, - TestBLRUs, - TestBLRUm, - TestBLRUl, - TestBLRUxl, - ) - for n in nmax - empty!(lru) - @printf(" %s, %d items\n", lru, n) - print(" Simple eviction: ") - for i in 1:n - str = get_str(i) - lru[str] = str - @assert lru.q[1].v == str - if isbounded(lru) && length(lru) >= lru.maxsize - tailstr = get_str(i-lru.maxsize+1) - @assert lru.q[end].v == tailstr +function lrutest() + #println("LRU consistency tests") + for lru in (TestLRU,TestBLRU) + for n in nmax + empty!(lru) + #@printf(" %s, %d items\n", lru, n) + #print(" Simple eviction: ") + for i in 1:n + str = get_str(i) + lru[str] = str + @assert lru.q[1].v == str + if isbounded(lru) && length(lru) >= lru.maxsize + tailstr = get_str(i-lru.maxsize+1) + @assert lru.q[end].v == tailstr + end end - end - println("pass") + #println("pass") - print(" Lookup, random access: ") - for i in 1:n - str = get_str(rand(1:n)) - if haskey(lru, str) # the bounded LRUs can have cache misses - blah = lru[str] - @assert lru.q[1].v == blah + #print(" Lookup, random access: ") + for i in 1:n + str = get_str(rand(1:n)) + if haskey(lru, str) # the bounded LRUs can have cache misses + blah = lru[str] + @assert lru.q[1].v == blah + end end + #println("pass") end - println("pass") + empty!(lru) end - empty!(lru) end + +lrutest() diff --git a/examples/modint.jl b/examples/modint.jl index 74a5c3aabd355..9bc9708760420 100644 --- a/examples/modint.jl +++ b/examples/modint.jl @@ -1,3 +1,5 @@ +module ModInts + importall Base immutable ModInt{n} <: Integer @@ -15,3 +17,5 @@ promote_rule{n}(::Type{ModInt{n}}, ::Type{Int}) = ModInt{n} show{n}(io::IO, k::ModInt{n}) = print(io, "$(k.k) mod $n") showcompact(io::IO, k::ModInt) = print(io, k.k) + +end # module \ No newline at end of file diff --git a/examples/quaternion.jl b/examples/quaternion.jl index 6f0f24477b15b..afd199124c37e 100644 --- a/examples/quaternion.jl +++ b/examples/quaternion.jl @@ -1,3 +1,5 @@ +module Quaternions + import Base: convert, promote_rule, show, real, imag, conj, abs, abs2, inv, +, -, /, * immutable Quaternion{T<:Real} <: Number @@ -16,6 +18,7 @@ convert{T}(::Type{Quaternion{T}}, z::Complex) = convert{T}(::Type{Quaternion{T}}, z::Quaternion) = Quaternion(convert(T,z.q0), convert(T,z.q1), convert(T,z.q2), convert(T,z.q3)) +promote_rule{s,S}(::Type{MathConst{s}}, ::Type{Quaternion{S}}) = Quaternion{S} promote_rule{T,S}(::Type{Complex{T}}, ::Type{Quaternion{S}}) = Quaternion{promote_type(T,S)} promote_rule{S}(::Type{Bool}, ::Type{Quaternion{S}}) = Quaternion{S} promote_rule{T<:Real,S}(::Type{T}, ::Type{Quaternion{S}}) = Quaternion{promote_type(T,S)} @@ -48,9 +51,4 @@ inv(z::Quaternion) = conj(z)/abs2(z) z.q0*w.q3 + z.q1*w.q2 - z.q2*w.q1 + z.q3*w.q0) (/)(z::Quaternion, w::Quaternion) = z*inv(w) -q = Quaternion(1,0,0,0) -x = Quaternion(0,1,1,1) - -println("q = $q") -println("q*2.0+2 = $(q*2.0+2)") -println("abs((-q+x*2)/4) = ", abs((-q+x*2)/4)) +end # module \ No newline at end of file diff --git a/examples/queens.jl b/examples/queens.jl index 89026f6e76397..b485449b5b293 100644 --- a/examples/queens.jl +++ b/examples/queens.jl @@ -1,9 +1,9 @@ addqueen(queens::Array{Vector{Int}}, queen::Vector{Int}) = push!(copy(queens), queen) -hitsany(queen::Vector{Int}, queens::Array{Vector{Int}}) = any(map((x) -> hits(queen, x), queens)) +hitsany(queen::Vector{Int}, queens::Array{Vector{Int}}) = any(map(x->hits(queen, x), queens)) hits(a::Array{Int}, b::Array{Int}) = any(a .== b) || abs(a-b)[1] == abs(a-b)[2] -function solve(x::Int, y::Int, n::Int, d::Array{Vector{Int}}) +function solve(x, y, n, d=Array(Vector{Int}, 0)) if n == 0 return d end @@ -11,19 +11,9 @@ function solve(x::Int, y::Int, n::Int, d::Array{Vector{Int}}) for py = 1:y if !hitsany([px, py], d) s = solve(x, y, n-1, addqueen(d, [px, py])) - if !isequal(s, None) - return s - end + s != nothing && return s end end end - return None -end - -solve(x, y, n) = solve(x, y, n, Array(Vector{Int}, 0)) - -for i = 1:8 - print("Solve for $i\n") - print(solve(8, 8, i)) - print("\n") + return nothing end diff --git a/examples/typetree.jl b/examples/typetree.jl index 9ca30b6eaace5..ae5443945867a 100644 --- a/examples/typetree.jl +++ b/examples/typetree.jl @@ -1,3 +1,4 @@ +module TypeTrees ## # Generate a text graphic of Julia modules type tree ## @@ -112,9 +113,10 @@ end # TODO: option to list subtrees of type tree, or other symbol types const types_tree = Dict{String, TTNode}() -for m = [Base, Core, Main] +for m in (Base, Core, Main) store_all_from(m) end -print_tree(types_tree) +# print_tree(types_tree) +end # module \ No newline at end of file diff --git a/examples/wordcount.jl b/examples/wordcount.jl index 321d296f04109..150a1b21b9c6e 100644 --- a/examples/wordcount.jl +++ b/examples/wordcount.jl @@ -70,16 +70,17 @@ end # Takes the name of a result file, and a list of input file names. # Combines the contents of all files, then performs a parallel_wordcount # on the resulting string. Writes the results to result_file. -function wordcount_files(result_file,input_file_names...) - text="" - for f in input_file_names - fh=open(f) - text=join( {text,readall(fh)}, "\n" ) - close(fh) +function wordcount_files(result_file,inputs...) + text = "" + for file in inputs + open(file) do f + text *= readall(f) + end end - wc=parallel_wordcount(text) - fo=open(result_file,"w") - for (k,v) in wc - println(fo,k,"=",v) + wc = parallel_wordcount(readall(f)) + open(result_file,"w") do f + for (k,v) in wc + println(fo,k,"=",v) + end end end diff --git a/test/examples.jl b/test/examples.jl new file mode 100644 index 0000000000000..fa59909da7ebb --- /dev/null +++ b/test/examples.jl @@ -0,0 +1,42 @@ +include("../examples/bubblesort.jl") +a = rand(1:100,100) +@test issorted(sort!(a;alg=BubbleSort)) + +include("../examples/enum.jl") +@enum TestEnum TestEnum1 TestEnum2 TestEnum3 +@test [TestEnum1.n,TestEnum2.n,TestEnum3.n] == [0,1,2] + +include("../examples/lru.jl") +include("../examples/lru_test.jl") + +include("../examples/modint.jl") +b = ModInts.ModInt{10}(2) +c = ModInts.ModInt{10}(4) +@test b + c == ModInts.ModInt{10}(6) +@test c - b == ModInts.ModInt{10}(2) + +include("../examples/ndgrid.jl") +r = repmat(1:10,1,10) +r1, r2 = ndgrid(1:10, 1:10) +@test r1 == r +@test r2 == r' +r3, r4 = meshgrid(1:10,1:10) +@test r3 == r' +@test r4 == r + +include("../examples/quaternion.jl") +q = Quaternions.Quaternion(1,0,0,0) +x = Quaternions.Quaternion(0,1,1,1) +@test q*2.0+2 == Quaternions.Quaternion(4,0,0,0) +@test abs((-q+x*2)/4) == 0.9013878188659973 + +include("../examples/queens.jl") +@test solve(8, 8, 1) == Array{Int,1}[[1,1]] +@test solve(8, 8, 7) == Array{Int,1}[[1,1],[2,3],[3,5],[4,2],[5,8],[7,4],[8,7]] + +# At least make sure code laods +include("../examples/plife.jl") + +include("../examples/preduce.jl") + +include("../examples/wordcount.jl") \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 042680fb4cfc8..b63dda254fc69 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,7 +8,7 @@ testnames = [ "resolve", "pollfd", "mpfr", "broadcast", "complex", "socket", "floatapprox", "readdlm", "regex", "float16", "combinatorics", "sysinfo", "rounding", "ranges", "mod2pi", "euler", "show", - "lineedit", "replcompletions", "repl", "test" + "lineedit", "replcompletions", "repl", "test", "examples" ] @unix_only push!(testnames, "unicode")