From 71a36fd078b721df4180fc3c05b1421095de9dca Mon Sep 17 00:00:00 2001 From: Andy Hayden Date: Mon, 5 Jan 2015 01:44:31 +0000 Subject: [PATCH 1/4] TST use with statements in bese64 --- test/base64.jl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/base64.jl b/test/base64.jl index 5c360b6ced05c..0ec6ac960ced0 100644 --- a/test/base64.jl +++ b/test/base64.jl @@ -9,16 +9,17 @@ ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=""" # Encode and decode fname = tempname() -f = open(fname, "w") -opipe = Base64EncodePipe(f) -write(opipe,inputText) -close(opipe) -close(f) -f = open(fname, "r") -ipipe = Base64DecodePipe(f) -@test readall(ipipe) == inputText -close(ipipe) -close(f) +open(fname, "w") do f + opipe = Base64EncodePipe(f) + write(opipe,inputText) + close(opipe) +end + +open(fname, "r") do f + ipipe = Base64DecodePipe(f) + @test readall(ipipe) == inputText + close(ipipe) +end rm(fname) # Encode to string and decode From ff0470cfe5b94b62db66aa24a431026032bee34c Mon Sep 17 00:00:00 2001 From: Andy Hayden Date: Mon, 5 Jan 2015 09:08:23 +0000 Subject: [PATCH 2/4] TST more combinatorics tests --- test/combinatorics.jl | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/test/combinatorics.jl b/test/combinatorics.jl index df1c5e2eaecc5..0e3bd5cc3e559 100644 --- a/test/combinatorics.jl +++ b/test/combinatorics.jl @@ -1,20 +1,37 @@ -@test factorial(7) == 5040 -@test factorial(7,3) == 7*6*5*4 @test binomial(5,-1) == 0 @test binomial(5,10) == 0 @test binomial(5,3) == 10 +@test binomial(2,1) == 2 +@test binomial(1,2) == 0 +@test binomial(-2,1) == -2 +@test binomial(2,-1) == 0 + +#Issue 6154 +@test binomial(int32(34), int32(15)) == binomial(BigInt(34), BigInt(15)) == 1855967520 +@test binomial(int64(67), int64(29)) == binomial(BigInt(67), BigInt(29)) == 7886597962249166160 +@test binomial(int128(131), int128(62)) == binomial(BigInt(131), BigInt(62)) == 157311720980559117816198361912717812000 +@test_throws InexactError binomial(int64(67), int64(30)) + p = shuffle([1:1000]) @test isperm(p) @test all(invperm(invperm(p)) .== p) + push!(p, 1) @test !isperm(p) + a = randcycle(10) @test ipermute!(permute!([1:10], a),a) == [1:10] + +@test collect(combinations("abc",3)) == ["abc"] @test collect(combinations("abc",2)) == ["ab","ac","bc"] +@test collect(combinations("abc",1)) == ["a","b","c"] +@test collect(combinations("abc",0)) == [""] @test collect(permutations("abc")) == ["abc","acb","bac","bca","cab","cba"] + @test collect(filter(x->(iseven(x[1])),permutations([1,2,3]))) == Any[[2,1,3],[2,3,1]] @test collect(filter(x->(iseven(x[3])),permutations([1,2,3]))) == Any[[1,3,2],[3,1,2]] @test collect(filter(x->(iseven(x[1])),combinations([1,2,3],2))) == Any[[2,3]] + @test collect(partitions(4)) == Any[[4], [3,1], [2,2], [2,1,1], [1,1,1,1]] @test collect(partitions(8,3)) == Any[[6,1,1], [5,2,1], [4,3,1], [4,2,2], [3,3,2]] @test collect(partitions(8, 1)) == Any[[8]] @@ -36,18 +53,16 @@ for n = 0:7, k = 1:factorial(n) @test nthperm(p) == k end -#Issue 6154 -@test binomial(int32(34), int32(15)) == binomial(BigInt(34), BigInt(15)) == 1855967520 -@test binomial(int64(67), int64(29)) == binomial(BigInt(67), BigInt(29)) == 7886597962249166160 -@test binomial(int128(131), int128(62)) == binomial(BigInt(131), BigInt(62)) == 157311720980559117816198361912717812000 - -# issue #6579 +@test factorial(7) == 5040 +@test factorial(7,3) == 7*6*5*4 @test factorial(0) == 1 +@test_throws DomainError factorial(-1) + @test factorial(int64(20)) == 2432902008176640000 +# issue #6579 @test_throws OverflowError factorial(int64(21)) -@test_throws DomainError factorial(-1) @test typeof(factorial(int8(2))) == typeof(factorial(int8(1))) if Int === Int32 -@test factorial(int32(12)) === int32(479001600) -@test_throws OverflowError factorial(int32(13)) + @test factorial(int32(12)) === int32(479001600) + @test_throws OverflowError factorial(int32(13)) end From 3d376d18e1398309afef73a83066d319fe020090 Mon Sep 17 00:00:00 2001 From: Andy Hayden Date: Mon, 5 Jan 2015 13:42:01 +0000 Subject: [PATCH 3/4] TST intfuncs more coverage --- test/intfuncs.jl | 73 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/test/intfuncs.jl b/test/intfuncs.jl index 8700b6088aa7f..e7cae0ef1ebcd 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -1,5 +1,52 @@ -# issue #8266 +@test gcd(3, 5) == 1 +@test gcd(3, 15) == 3 +@test gcd(0, 15) == 15 +@test gcd(3, -15) == 3 +@test gcd(-3, -15) == 3 +@test gcd(0, 0) == 0 + +@test gcd(2, 4, 6) == 2 + +@test typeof(gcd(int32(3), int32(15))) == Int32 + +@test lcm(2, 3) == 6 +@test lcm(4, 6) == 12 +@test lcm(3, 0) == 0 +@test lcm(0, 0) == 0 +@test lcm(4, -6) == 12 +@test lcm(-4, -6) == 12 + +@test lcm(2, 4, 6) == 12 + +@test typeof(lcm(int32(2), int32(3))) == Int32 + +@test gcdx(5, 12) == (1, 5, -2) +@test gcdx(5, -12) == (1, 5, 2) +@test gcdx(-25, -4) == (1, -1, 6) + +@test invmod(6, 31) == 26 +@test invmod(-1, 3) == 2 +@test invmod(-1, -3) == 2 +@test_throws ErrorException invmod(0, 3) + +@test powermod(2, 3, 5) == 3 +@test powermod(2, 3, -5) == -2 +@test_throws DomainError powermod(2, -3, 5) + +@test nextpow2(3) == 4 +@test nextpow(2, 3) == 4 +@test nextpow(2, 4) == 4 +@test nextpow(2, 7) == 8 +@test_throws DomainError nextpow(0, 3) +@test_throws DomainError nextpow(3, 0) +@test prevpow2(3) == 2 +@test prevpow(2, 4) == 4 +@test prevpow(2, 5) == 4 +@test_throws DomainError prevpow(0, 3) +@test_throws DomainError prevpow(0, 3) + +# issue #8266 @test ndigits(-15, 10) == 2 @test ndigits(-15, -10) == 2 @test ndigits(-1, 10) == 1 @@ -14,3 +61,27 @@ @test ndigits(146, -3) == 5 +@test bin(3) == "11" +@test bin(3, 2) == "11" +@test bin(3, 3) == "011" +@test bin(-3) == "-11" +@test bin(-3, 3) == "-011" + +@test oct(9) == "11" +@test oct(-9) == "-11" + +@test dec(121) == "121" + +@test hex(12) == "c" +@test hex(-12, 3) == "-00c" +@test num2hex(1243) == "00000000000004db" + +@test base(2, 5, 7) == "0000101" + +@test bits(1035) == "0000000000000000000000000000000000000000000000000000010000001011" + +@test digits(4, 2) == [0, 0, 1] +@test digits(5, 3) == [2, 1] + +@test isqrt(4) == 2 +@test isqrt(5) == 2 From 93f6ccbd50f3641a1c0dbd95c3c29404ac944428 Mon Sep 17 00:00:00 2001 From: Andy Hayden Date: Mon, 5 Jan 2015 15:21:39 +0000 Subject: [PATCH 4/4] TST fix 32bit bits test, add comment to interesting binomial result --- test/combinatorics.jl | 2 +- test/intfuncs.jl | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/combinatorics.jl b/test/combinatorics.jl index 0e3bd5cc3e559..c6d3c8ce9e7a5 100644 --- a/test/combinatorics.jl +++ b/test/combinatorics.jl @@ -3,7 +3,7 @@ @test binomial(5,3) == 10 @test binomial(2,1) == 2 @test binomial(1,2) == 0 -@test binomial(-2,1) == -2 +@test binomial(-2,1) == -2 # let's agree @test binomial(2,-1) == 0 #Issue 6154 diff --git a/test/intfuncs.jl b/test/intfuncs.jl index e7cae0ef1ebcd..8d1fce1084df6 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -74,11 +74,12 @@ @test hex(12) == "c" @test hex(-12, 3) == "-00c" -@test num2hex(1243) == "00000000000004db" +@test num2hex(1243) == (Int == Int32 ? "000004db" : "00000000000004db") @test base(2, 5, 7) == "0000101" -@test bits(1035) == "0000000000000000000000000000000000000000000000000000010000001011" +@test bits(1035) == (Int == Int32 ? "00000000000000000000010000001011" : + "0000000000000000000000000000000000000000000000000000010000001011") @test digits(4, 2) == [0, 0, 1] @test digits(5, 3) == [2, 1]