by throwawaylinux · 5y ago · 0 comments
void fn(int a, int b)
{
if (a == b)
printf("equal");
else
printf("not);
}
What are the 3 test cases you would write? What are the 9? fn(1, 1) -> "equal"
fn(1, 0) -> "not"
What more useful tests are there here? Other than just picking arbitrary numbers, maxint/minint/1/0/-1/etc are not significant edge cases here.Let's add 1 to a before the equality comparison in order to meet a new business requirement. fn(1, 1) still works, but does fn(maxint, maxint)? Or does it suddenly throw an exception (or worse, silently roll over to minint)?
I'm not saying more tests can't catch specific bugs you might come up with, I'm asking how you can choose numbers that have fundamental edge cases for this specific requirement without looking at the actual implementation. I don't think you really can. maxint/minint/0/-1/1 are generally common choices but only because they do tend to catch signdness errors or inequalities or division by zero or whatever, they aren't really fundamental to the requirement of this function though. If we just wanted to blindly blast those numbers in, it would be 25 individual test cases for all permutations. Doesn't seem to be reasonable.
Proving the function is correct for the known edge cases for integers and integer comparison is not excessively hard. Let's go for the worst case of edge cases, and:
- Pick the input pairs that are problems with ints: the set of (minint, maxint).
- Pick the input pairs that are problems with comparisons: the set of (-1, 0, 1).
- Pick input pairs that show that it's correct where a < b, a = b, and a > b. That last set of cases is nicely covered by the previous two.
That's a total of 25 input pairs(5^2). In my opinion, that's not an unreasonable number of test cases to test for correctness. Why do I think it's reasonable? Because while it's technically 25 test cases, it's really just one test that runs through an array of input values in a loop. Really simple, and adding more test cases as the business logic changes is equally simple.
And let's be honest, given how quickly computers work these days, even testing all ints for both inputs isn't completely unreasonable (though I wouldn't make it a part of a unit test suite). Especially if your logic is more complicated than a simple equality test (which most function logic will be).
EDIT: "Accidentally mistype" 'a==b' as '(a == 0 || a == 1 || a == maxint) && a == b'
Love it.
The problem is not this one particular function, it's transferring this methodology to something more complex. A reasonable set of tests that does not aim to prove correctness is quite entitled to assume a reasonable implementation. You wouldn't have to arbitrarily re-run all your test cases with values incremented by 42 just in case someone incorrectly subtracted 42 somewhere, for example.
What you want is a specification of the behavior, and to verify that the implementation satisfies the specification. This will be proven in the general case, and then you don't need to think about individual cases.
And yes I'm aware of formal methods, and agree they seem like the better way to go for proving.
a == b
And (in Java) a.equals(b)
In order to catch misguided refactorings. (I’ve introduced bugs this way before I learned that == can give a false positive for strings if the strings are interned) void fn(int a, int b)
{
string ret = "not";
if (a == b)
ret = "equal";
printf(ret);
}
*I understand, this is not a good optimization in every language.Though I'd probably write:
void fn(int a, int b)
{
printf(a == b ? "equal" : "not");
}Though I would also accept:
void fn(int const a, int const b)
{
printf(a == b ? "equal" : "not");
}
to avoid duplicating the call to printf(). If this were Rust I might suggest: fn theFn(a: isize, b: isize)
{
let ret;
if a == b {
ret = "equal";
} else {
ret = "not";
}
println!("{}", ret);
}
but only because Rust, unlike C or C++, will enforce that `ret` is assigned exactly once before it is used.This is not about obviousness. You can write the same expression lots of different ways, which are all obvious. This is still a question about testable paths and code hygiene.
Nesting the same function call in both branches of if/else unintentionally obscures that it's not a conditional printf call.
If you're going to write:
printf(a == b ? "equal" : "not");
you don't need the function at all, completely missing the point.
Your second example is definitely better in Rust.
And the version with mutation masks the fact that the initial value assigned to `ret` may or may not be used, which seems like a very similar problem to me. Plus it involves mutation, which IMHO significantly increases the complexity of any code. Unfortunately C offers limited options here; as cryptic and controversial as it may be, the ternary operator is the only (non-hackish) way to make an expression which selects between two or more values based on a boolean condition without either the possibility of failing to assign a value or requiring a "default" value which at least some paths through the function won't use. (What if there is no reasonable default value and all the required branches are expensive to compute?)
> If you're going to write: … printf(a == b ? "equal" : "not"); … you don't need the function at all, completely missing the point.
The function still encapsulates the specific comparison and the way the result is reported—exactly like the other versions—so I wouldn't say it's completely useless despite containing only one line of code. This is, of course, merely a toy example. You could assign the string to a named const variable if you wanted to make it slightly more self-documenting, though I question whether that would make this particular code any more readable.
This is a quirk of human text comprehension. In a small program, it seems arbitrary because you can see it all at once. Make the branches do more than simply printf and you can see why you want to frontload the else, rather than pick through the if/else to see what always happens.
int pow2(int v, int pow)
{
int result;
if (pow >= 0)
result = v << pow;
else
result = v >> -pow;
return result;
}
Looking at this, both sides of the if/else perform a multiply by 2^pow, you can't take the operation to possibly not happen.So I agree factoring common operations can be a good thing, but I think it's really a case by case basis and my code is fine.
I would also caution against assuming something may not happen elsewhere in a function just because you see it inside a conditional block. This happens frequently to various degrees and I've never encountered a style policy preventing it or any compiler or linter warnings for it.