Home > database >  How to test if two codes give exactly the same output for any input
How to test if two codes give exactly the same output for any input

Time:07-06

I've been solving some questions in python at college.

Unfortunately the system we use for submition don't give exactly which tests we failed.

That makes impossible to find out where my logic is wrong even if I have a solution in my hand.

I never used testing and I not even know how it works, but I'd like to know if there is any way to find out if two codes give exactly the same output for any input I give. So I can get the solutions and find out where I did wrong.

PS: I'm not cheating, I'm just getting the provided solutions at the end of the tests and trying to understand where my logic is wrong, so I can do better in the next ones.

For example: in this leetcode problem: https://leetcode.com/problems/roman-to-integer/

This was my solution:

prefix = ['I', 'X', 'C']
value = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}

def rec(s, v):
    n = s[0:1]
    if len(s) > 1 and n in prefix and value[s[0:1]] < value[s[1:2]]:
        n = s[0:2]
    if len(n) > 1:
        v  = value[n[1]] - value[n[0]]
    else:
        v  = value[n]
    s = s.removeprefix(n)
    return v if not s else rec(s, v)


class Solution:
    def romanToInt(self, s: str) -> int:
        return (rec(s, 0))

But I solved As well with this other code:

class Solution:
    def romanToInt(self, s: str) -> int:
        prefix = ['I', 'X', 'C']
        value = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}

        v = 0
        while s:
            n = s[0:1]
            if len(s) > 1 and n in prefix and value[s[0:1]] < value[s[1:2]]:
                n = s[0:2]
            if len(n) > 1:
                v  = value[n[1]] - value[n[0]]
            else:
                v  = value[n]
            print(s, n, v)
            s = s.removeprefix(n)
        return v

Let's say that I couldn't solve it, but I have the solution, How could I compare if the output for my code in progress is the same of the solution ? So I can check where I'm doing wrong in my logic.

CodePudding user response:

To answer your general question

This goes into a large topics in testing methodologies and strategies. I'll give a short list of different testing techniques that can achieve different goals that are related to what you're looking for. Google to find out more.

Example/Case testing

Manually select testcases to test -- most common/natural test. Typically choose cases that represent ideal use-cases as well as known edge cases. Simple, reproducible, but weak to missing cases you didn't consider.

Property testing

Conceptually test the invariant constraints of a system rather than specific cases, e.g. for addition, you'd test that 2 3 == 3 2 (commutative), (2 3) 4 == 2 (3 4) (associative), and that zero is the addition identity. These tests work regardless of any specific case, so test cases could even be generated.

Exhaustive testing

Computers are finite machines after all. It's possible to enumerate some/most/all possible inputs. E.g. 8-bit integer addition can be exhaustively tested in 256 * 256 = 65,536 cases. Can become extremely (exponentially) expensive and impractical.

Fuzzing

Throwing random input into the system under test to see if there are previously uncovered edge cases.

Mutation testing

Tests the coverage of your tests by randomly modifying the system under test and confirming that tests fail.

To solve your specific issue with that leetcode problem

To be 99% exhaustive, you could use a combination of the first 3 I listed above.

  1. Example testing: test basic cases, then more complex ones.
  2. Property testing: write a number to roman numeral, test that the two conversions perfectly reverse each other.
  3. Exhaustive testing: test all numbers from 1 to some big number with your number to roman numeral. Computers are fast, you can probably get to 1M at least.

CodePudding user response:

Can you share code sample of a related work? (NOT the one you are submitting at college). They could help in answering your question.

  • Related