i have a piece of code in which i have an input file named Small_Input.txt and in that input file i have a lot of tests. for each test case my code is trying to find whether there exists three no whose sum is equal to a given number. for this i have to write tests using rspec i tried to write but as i am new to this one and didn't get any good article about how to test nested methods. here is my code
class Solution
#main
def has_sum? (input_file_name)
#input_file_name = gets.strip
input_file = File.exist?(input_file_name) ? IO.readlines(input_file_name) : nil
#binding.pry
output_file_name = (input_file_name[0] == 'S' || input_file_name[0] == 's') ? "Small_Output.txt" : "Big_Output.txt"
File.truncate(output_file_name, 0) if File.exist?(output_file_name)
if input_file.nil? || input_file.size() < 1
puts("Error there is no data in input file or file doesn't exists")
return -1
else
solve_for_given_input_file(input_file)
end
end
def solve_for_given_input_file(input_file)
line_no = 0
test_cases = input_file[line_no].to_i
line_no += 1
for i in 1..test_cases
number_of_ele = input_file[line_no].to_i
line_no += 1
array = input_file[line_no].split(' ').map(&:to_i)
line_no += 1
target = input_file[line_no].to_i
line_no += 1
k = 0
k = k + solve(input_file_name, number_of_ele, array, target)
end
end
def solve(input_file_name, number_of_ele, array, target)
array.sort!
u = 0
solution = nil
for i in 0..number_of_ele
ptr1 = i + 1
ptr2 = number_of_ele - 1
while ptr1 < ptr2 do
sum = array[i] + array[ptr1] + array[ptr2]
if(sum == target)
solution = update_solution(solution, array[i], array[ptr1], array[ptr2])
end
if(sum > target)
ptr2 -= 1
else
ptr1 += 1
end
end
end
output_file_name = (input_file_name[0] == 'S' || input_file_name[0] == 's') ? "Small_Output.txt" : "Big_Output.txt"
output_file = File.new(output_file_name, "a+")
if(solution.nil?)
output_file.write("Solution Doesn't exists\n")
else
output_file.write(solution[0].to_s + " " + solution[1].to_s + " " + solution[2].to_s + "\n")
u=1
end
output_file.close
return u
end
def update_solution(solution, value1, value2, value3)
if solution.nil? || (solution[0] > value1)
solution = Array.new(3)
solution[0] = value1
solution[1] = value2
solution[2] = value3
elsif solution[1] > value2 || (solution[1] == value2 && solution[2] > value3)
solution[0] = value1
solution[1] = value2
solution[2] = value3
end
return solution
end
end
and i have written some test which may be wrong or not written properly require 'problem'
describe Solution do
it "should detect when file does not exist" do
sa = Solution.new
test_string = 'spec/no_file.txt'
expect(sa.has_sum? test_string).to be -1
end
it "should detect when a string contains sum" do
sa = Solution.new
test_string = 'spec/Small_Input.txt'
expect(sa.has_sum? test_string).to be true
end
it "should detect when a string doesn't contain sum" do
sa = Solution.new
test_string = 'spec/Small_Input.txt'
expect(sa.has_sum? test_string).to be false
end
end
as i am john snow(who knows nothing) in rspec any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire