Skip to content

Commit

Permalink
Refine test
Browse files Browse the repository at this point in the history
  • Loading branch information
universato committed May 17, 2021
1 parent e453c7f commit 6cfb8ed
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 59 deletions.
12 changes: 6 additions & 6 deletions test/convolution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def test_random_array_modulo_default
max_num = 10**18
conv = Convolution.new
20.times{
a = (0 .. rand(100)).map{ rand(-max_num .. max_num) }
b = (0 .. rand(100)).map{ rand(-max_num .. max_num) }
a = (0 .. 20).map{ rand(-max_num .. max_num) }
b = (0 .. 20).map{ rand(-max_num .. max_num) }
assert_equal convolution_naive(a, b, 998_244_353), conv.convolution(a, b)
}
end
Expand All @@ -47,8 +47,8 @@ def test_random_array_modulo_NTT_friendly_given_proot
[ 924_844_033, 924_844_028]].each{ |mod, proot|
conv = Convolution.new(mod, proot)
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
b = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 20).map{ rand(-max_num .. max_num) }
b = (0 ... 20).map{ rand(-max_num .. max_num) }
assert_equal convolution_naive(a, b, mod), conv.convolution(a, b)
}
}
Expand All @@ -59,8 +59,8 @@ def test_random_array_modulo_NTT_friendly_not_given_proot
[998_244_353, 1_012_924_417, 924_844_033].each{ |mod|
conv = Convolution.new(mod)
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
b = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 20).map{ rand(-max_num .. max_num) }
b = (0 ... 20).map{ rand(-max_num .. max_num) }
assert_equal convolution_naive(a, b, mod), conv.convolution(a, b)
}
}
Expand Down
12 changes: 6 additions & 6 deletions test/crt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class CrtTest < Minitest::Test
def test_two_elements
[*1 .. 20].repeated_permutation(2){ |a, b|
[*-10 .. 10].repeated_permutation(2){ |c, d|
[*1 .. 5].repeated_permutation(2){ |a, b|
[*-4 .. 4].repeated_permutation(2){ |c, d|
rem, mod = crt([c, d], [a, b])
if mod == 0
assert (0 ... a.lcm(b)).none?{ |x| x % a == c && x % b == d }
Expand All @@ -22,8 +22,8 @@ def test_two_elements
end

def test_three_elements
[*1 .. 5].repeated_permutation(3){ |a, b, c|
[*-5 .. 5].repeated_permutation(3){ |d, e, f|
[*1 .. 4].repeated_permutation(3){ |a, b, c|
[*-4 .. 4].repeated_permutation(3){ |d, e, f|
rem, mod = crt([d, e, f], [a, b, c])
lcm = [a, b, c].reduce :lcm
if mod == 0
Expand All @@ -43,9 +43,9 @@ def test_random_array
max_num = 10**18
20.times{
ans = rand(max_ans)
m = (0 ... 1000).map{ rand(1 .. max_num) }
m = (0 ... 20).map{ rand(1 .. max_num) }
r = m.map{ |m| ans % m }
mod = m.reduce :lcm
mod = m.reduce(:lcm)
assert_equal [ans % mod, mod], crt(r, m)
}
end
Expand Down
20 changes: 11 additions & 9 deletions test/dsu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def test_simple
end

def test_line
n = 500_000
n = 30
uf = DSU.new(n)
(n - 1).times { |i| uf.merge(i, i + 1) }
assert_equal n, uf.size(0)
assert_equal 1, uf.groups.size
end

def test_line_reverse
n = 500_000
n = 30
uf = DSU.new(n)
(n - 2).downto(0) { |i| uf.merge(i, i + 1) }
assert_equal n, uf.size(0)
Expand Down Expand Up @@ -97,15 +97,17 @@ def test_aizu_sample_false
end

def test_rand_isoration
n = 100
n = 30
uft = UnionFind.new(n)
n.times do
a = rand(n)
b = rand(n)
next if a == b

assert !uft.same?(a, b)
values = [0, 1, 2, 3, 5, 10]
values.product(values) do |a, b|
if a == b
assert uft.same?(a, b)
else
assert !uft.same?(a, b)
end
end
assert_equal Array.new(n){ |i| [i] }, uft.groups
end

def test_merge
Expand Down
2 changes: 1 addition & 1 deletion test/fenwick_tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_zero
end

def test_naive
(1 .. 50).each do |n|
(1 .. 20).each do |n|
fw = FenwickTree.new(n)
n.times { |i| fw.add(i, i * i) }
(0 .. n).each do |l|
Expand Down
2 changes: 1 addition & 1 deletion test/floor_sum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def floor_sum_naive(n, m, a, b)

class FloorSumTest < Minitest::Test
def test_floor_sum
k = 20
k = 5
(0..k).each do |n|
(1..k).each do |m|
(0..k).each do |a|
Expand Down
4 changes: 2 additions & 2 deletions test/inv_mod_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def test_inv_hand
end

def test_inv_mod
(-100 .. 100).each do |a|
(1 .. 1000).each do |b|
(-10 .. 10).each do |a|
(1 .. 30).each do |b|
next unless 1 == (a % b).gcd(b)

c = inv_mod(a, b)
Expand Down
6 changes: 3 additions & 3 deletions test/lcp_array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class LcpArrayTest < Minitest::Test
def test_random_array_small_elements
max_num = 5
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 30).map{ rand(-max_num .. max_num) }
assert_equal lcp_array_naive(a), lcp_array(a, suffix_array(a))
}
end

def test_random_array_big_elements
max_num = 10**18
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 30).map{ rand(-max_num .. max_num) }
assert_equal lcp_array_naive(a), lcp_array(a, suffix_array(a))
}
end

def test_random_string
20.times{
s = (0 ... 100).map{ rand(' '.ord .. '~'.ord).chr }.join
s = (0 ... 30).map{ rand(' '.ord .. '~'.ord).chr }.join
assert_equal lcp_array_naive(s), lcp_array(s, suffix_array(s))
}
end
Expand Down
26 changes: 10 additions & 16 deletions test/modint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# test ModInt
class ModIntTest < Minitest::Test
def setup
@mods = [1, 2, 3, 6, 10, 11, 2**16, 119 * 2**23 + 1, 10**9 + 7]
@primes = [2, 3, 5, 7, 11, 10**9 + 7]
@values = [-10**5, -6, -2, -1, 0, 1, 2, 3, 6, 10**100]
@mods = [1, 2, 3, 6, 10, 11, 1_000_000_007]
@primes = [2, 3, 5, 7, 1_000_000_007]
@values = [-100_000, -2, -1, 0, 1, 2, 10_000_000_000]
end

def test_example
Expand Down Expand Up @@ -178,9 +178,9 @@ def test_pow
end

def test_pow_method
mods = [2, 3, 10, 17, 2**16, 119 * 2**23 + 1, 10**9 + 7]
xs = [-6, -2, -1, 0, 1, 2, 6, 100, 10**9 + 7]
ys = [0, 1, 2, 3, 6, 10, 10**9, 10**100]
mods = [2, 3, 10, 1_000_000_007]
xs = [-6, -2, -1, 0, 1, 2, 1_000_000_007]
ys = [0, 1, 2, 1_000_000_000]

mods.each do |mod|
ModInt.mod = mod
Expand Down Expand Up @@ -209,9 +209,7 @@ def test_inv_in_the_case_that_mod_is_prime
@primes.each do |prime_mod|
ModInt.mod = prime_mod

values = Array.new(30) { rand(-10**100...10**100) }
values.concat(Array(-6..6))
values.each do |value|
@values.each do |value|
next if (value % prime_mod).zero?

expected = value.to_bn.mod_inverse(prime_mod).to_i
Expand All @@ -224,9 +222,7 @@ def test_inv_in_the_case_that_mod_is_prime
end

def test_inv_in_the_random_mod_case
mods = Array.new(30) { rand(2..10**100) }
mods.concat(Array(2..30))

mods = [2, 3, 4, 5, 10, 1_000_000_007]
mods.each do |mod|
ModInt.mod = mod

Expand All @@ -245,9 +241,7 @@ def test_div_in_the_case_that_mod_is_prime
@primes.each do |prime_mod|
ModInt.mod = prime_mod

values = Array.new(30) { rand(-10**100...10**100) }
values.concat(Array(-6..6))
values.product(values) do |(x, y)|
@values.product(@values) do |(x, y)|
next if (y % prime_mod).zero?

expected = (x * y.to_bn.mod_inverse(prime_mod).to_i) % prime_mod
Expand All @@ -260,7 +254,7 @@ def test_div_in_the_case_that_mod_is_prime
end

def test_inv_in_the_case_that_mod_is_not_prime
mods = { 4 => [1, 3], 6 => [1, 5], 8 => [1, 3, 5, 7], 9 => [2, 4, 7, 8], 10 => [3, 7, 9] }
mods = { 4 => [1, 3], 6 => [1, 5], 9 => [2, 4, 7, 8], 10 => [3, 7, 9] }
mods.each do |(mod, numbers)|
ModInt.mod = mod

Expand Down
4 changes: 2 additions & 2 deletions test/pow_mod_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def naive_pow_mod(x, n, mod)

class PowModTest < Minitest::Test
def test_prime_mod
(-10 .. 10).each do |a|
(0 .. 10).each do |b|
(-5 .. 5).each do |a|
(0 .. 5).each do |b|
(2 .. 10).each do |c|
assert_equal naive_pow_mod(a, b, c), pow_mod(a, b, c)
# assert_equal naive_pow_mod(a, b, c), a.pow(b, c)
Expand Down
2 changes: 1 addition & 1 deletion test/segtree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_compare_naive
end
end

(0..30).each do |n|
(0..20).each do |n|
seg0 = SegtreeNaive.new(n, '$', &op)
seg1 = Segtree.new(n, '$', &op)

Expand Down
10 changes: 5 additions & 5 deletions test/suffix_array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ class SuffixArrayTest < Minitest::Test
def test_random_array_small_elements
max_num = 5
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 20).map{ rand(-max_num .. max_num) }
assert_equal suffix_array_naive(a), suffix_array(a)
}
end

def test_random_array_big_elements
max_num = 10**18
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 20).map{ rand(-max_num .. max_num) }
assert_equal suffix_array_naive(a), suffix_array(a)
}
end

def test_random_array_given_upper
max_num = 100
20.times{
a = (0 ... 100).map{ rand(0 .. max_num) }
a = (0 ... 20).map{ rand(0 .. max_num) }
assert_equal suffix_array_naive(a), suffix_array(a, max_num)
}
end

def test_random_array_calculated_upper
max_num = 100
20.times{
a = (0 ... 100).map{ rand(0 .. max_num) }
a = (0 ... 20).map{ rand(0 .. max_num) }
assert_equal suffix_array_naive(a), suffix_array(a, a.max)
}
end
Expand All @@ -50,7 +50,7 @@ def test_wrong_given_upper

def test_random_string
20.times{
s = (0 ... 100).map{ rand(' '.ord .. '~'.ord).chr }.join
s = (0 ... 20).map{ rand(' '.ord .. '~'.ord).chr }.join
assert_equal suffix_array_naive(s), suffix_array(s)
}
end
Expand Down
14 changes: 7 additions & 7 deletions test/z_algorithm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,44 @@ def z_algorithm_naive(s)
class ZAlgorithmTest < Minitest::Test
def test_random_string
20.times{
s = (0 ... 100).map{ rand(' '.ord .. '~'.ord).chr }.join
s = (0 ... 20).map{ rand(' '.ord .. '~'.ord).chr }.join
assert_equal z_algorithm_naive(s), z_algorithm(s)
}
end

def test_random_array_of_small_integer
max_num = 5
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 20).map{ rand(-max_num .. max_num) }
assert_equal z_algorithm_naive(a), z_algorithm(a)
}
end

def test_random_array_of_large_integer
max_num = 10**18
20.times{
a = (0 ... 100).map{ rand(-max_num .. max_num) }
a = (0 ... 20).map{ rand(-max_num .. max_num) }
assert_equal z_algorithm_naive(a), z_algorithm(a)
}
end

def test_random_array_of_char
20.times{
a = (0 ... 100).map{ rand(' '.ord .. '~'.ord).chr }
a = (0 ... 20).map{ rand(' '.ord .. '~'.ord).chr }
assert_equal z_algorithm_naive(a), z_algorithm(a)
}
end

def test_random_array_of_array
candidate = [[], [0], [1], [2], [0, 0], [1, 1], [2, 2]]
20.times{
a = (0 ... 100).map{ candidate.sample.dup }
a = (0 ... 20).map{ candidate.sample.dup }
assert_equal z_algorithm_naive(a), z_algorithm(a)
}
end

def test_repeated_string
max_n = 10**5
max_n = 30
20.times{
n = rand(1..max_n)
s = 'A' * n
Expand All @@ -65,7 +65,7 @@ def test_repeated_string
def test_unique_array
max_num = 10**18
20.times{
a = (0 ... 10**5).map{ rand(-max_num .. max_num) }.uniq
a = (0 ... 20).map{ rand(-max_num .. max_num) }.uniq
n = a.size
# [n, 0, 0, ..., 0]
assert_equal [n] + [0] * (n - 1), z_algorithm(a)
Expand Down

0 comments on commit 6cfb8ed

Please sign in to comment.