Skip to content

Instantly share code, notes, and snippets.

@beny
Created April 19, 2011 18:27
Show Gist options
  • Save beny/929169 to your computer and use it in GitHub Desktop.
Save beny/929169 to your computer and use it in GitHub Desktop.
testovaci skript pro projekt do PRL cislo 3
#!/usr/bin/ruby
require "benchmark"
DEBUG = false
# kontrola zda je mocnina dvojky
def is_pow?(x)
true if (x != 0) && ((x & (x - 1)) == 0);
end
# generovani cisel, vraci dekadicke verze vygenerovanych cisel
def generate(digits)
first = String.new
second = String.new
digits.times do
first << rand.round.to_s
second << rand.round.to_s
end
file = File.new("numbers", "w")
file.puts first
file.puts second
file.close
return first.to_i(2), second.to_i(2)
end
# prevod vystupu do binarniho retezce
def output_to_binary_string(string, digits)
result = Array.new
string.each_line do |line|
if line =~ /overflow/
result[0] = 1
next
end
position, value = line.split(":")
position = position.to_i - (digits-2)
result[position] = value.strip
end
result.join
end
# inicializace prostredi
puts "Mazu nepotrebne soubory ..." if DEBUG
%x(rm -fr ba numbers)
lib_path = "--prefix /usr/local/share/OpenMPI" if %x(hostname) =~ /merlin/
puts "Preklad programu ..." if DEBUG
%x(mpic++ #{lib_path} -o ba ba.cpp)
# zpracovani parametru
digits = ARGV[0].to_i
runs = ARGV[1].to_i
# test spravnosti parametru
abort "Spatne zadany pocet bitu, musi byt vetsi nez nula a mocnina dvou" if digits <= 0 or not is_pow?(digits)
abort "Pocet behu nemuze byt mensi nez 0" if runs < 0
# provedeni X behu a otestovani
ok = time_sum = 0
runs.times do |x|
puts "Generovani nahodnych cisel o #{digits} bitech ..." if DEBUG
first, second = generate digits
puts "Spusteni programu" if DEBUG
time = ("%.4f" % Benchmark.measure { @output = %x(mpirun #{lib_path} -np #{2*digits-1} ba)}.real).to_f
time_sum += time
result = output_to_binary_string(@output, digits).to_i(2)
if first+second == result
ok += 1
puts "Test #{x} in #{time}s ... OK"
puts "#{first} + #{second} = #{first+second} se rovna #{result}" if DEBUG
else
abort "Cisla #{first} + #{second} = #{first+second} program secetl spatne (!= result)"
end
end
puts "Celkem probehlo #{ok} testu spravne v prumernem realnem case #{"%.4f" % (time_sum/ok)}s"
# 4:0
# 3:1
# 5:1
# 6:1
# overflow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment