Skip to content

Commit

Permalink
Merge pull request #7400 from JuliaLang/jq/examples
Browse files Browse the repository at this point in the history
Clean up examples
  • Loading branch information
JeffBezanson committed Jun 28, 2014
2 parents 45c4952 + bcc425c commit f824b7e
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 72 deletions.
4 changes: 2 additions & 2 deletions examples/hpl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions examples/lru.jl
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -124,3 +125,5 @@ function delete!(lru::LRU, key)
delete!(lru.ht, key)
delete!(lru.q, idx)
end

end # module
69 changes: 32 additions & 37 deletions examples/lru_test.jl
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions examples/modint.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module ModInts

importall Base

immutable ModInt{n} <: Integer
Expand All @@ -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
10 changes: 4 additions & 6 deletions examples/quaternion.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module Quaternions

import Base: convert, promote_rule, show, real, imag, conj, abs, abs2, inv, +, -, /, *

immutable Quaternion{T<:Real} <: Number
Expand All @@ -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)}
Expand Down Expand Up @@ -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
18 changes: 4 additions & 14 deletions examples/queens.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
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
for px = 1:x
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
6 changes: 4 additions & 2 deletions examples/typetree.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module TypeTrees
##
# Generate a text graphic of Julia modules type tree
##
Expand Down Expand Up @@ -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
21 changes: 11 additions & 10 deletions examples/wordcount.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions test/examples.jl
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit f824b7e

Please sign in to comment.