0

I'm reading about Property based testing using Scala language. In this slide, they present this concept: For proving function a+b is true. we just only to prove those statements are true on random data:

  1. a + b = b + a
  2. a + 0 = a
  3. a + 1 + 1 = a + 2

My question is: Which methodologies for checking that our test cases are enough, and can cover all cases on different data. For example on previous example, how can we sure that after our three properties run correctly, we can sure that our implementation is right.

Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107

1 Answers1

0

First of all, I assume, you have a typo in #3, it's supposed to be + rather than *.

To answer your question, you most certainly can not be sure that your implementation is right if you prove these three properties. Consider this implementation for instance, that satisfies all three properties, but is definitely wrong:

def wrongPlus(a: Int, b: Int) = if(a < 3 || b <3) b a+b else 0

To definitively prove the (integer) addition, you need to have an independent implementation of next integer. Then, by definition:

1. a + 0 = a
2. a + next(b) = next(a + b) 

If these properties hold for any a and b and some operation +, then + is indeed the addition.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • can you tell me meaning of function `next`. moreover, are there any method that we know for sure we have made all conditions for property-based testing. thanks – Trần Kim Dự Jun 13 '17 at 03:25
  • `next` is just "+1". This is how natural numbers are defined. `1 = next(0)` is a natural number, and for each natural N there is exactly one M=next(N), that is also natural. The two properties above are definition of integer addition, so you know, that, if they are satisfied, your operation is addition by definition. There is no universal rule to figure out which properties to verify for any arbitrary operation. You are the only one who can your operation's properties. – Dima Jun 13 '17 at 10:45