I’m learning about unit tests, and have a doubt for a test i want to do,
to implement an “AND” logic gate
A B A^B
0 0 0
0 1 0
1 0 0
1 1 1
how can i test for a method that works like AND gate?, is this what a mock object is?
or stub?
Thanks,
Please provide pseudo code,
4
I assume you’re just looking for an example of how to write a unit test for something like an AND gate.
Depending on your unit test framework, you can use parameterized unit tests, like in NUnit:
[RowTest]
[Row(0,0,0)]
[Row(0,1,0)]
[Row(1,0,0)]
[Row(1,1,1)]
public void TestAndGate(int a, int b, int expected)
{
var test = new AndGateImplementation();
Assert.AreEqual(expected, test.And(a,b));
}
1
Unit tests verify that a function returns properly for a handful of inputs and known outputs. In the case of a simple logic function, you actually have the advantage of testing every input/output:
// this is what you're testing:
public Boolean And(Boolean p1, Boolean p2);
// depending on your testing framework, your test might look like this
public void TestAnd() {
TestAnd(true, true, true);
TestAnd(true, false, false);
TestAnd(false, true, false);
TestAnd(false, false, false);
}
public void TestAnd(Boolean p1, Boolean p2, Boolean r) {
Assert.Equal(And(p1, p2), r);
}