-
In this code, I am building a I know that we can set a breakpoint and kind of debug it with Is there any way to do this? import 'package:rohd/rohd.dart';
import 'package:test/test.dart';
class FullAdderModule extends Module {
FullAdderModule(
Logic a,
Logic b,
Logic cIn,
void Function(
Logic a, Logic b, Logic cIn, Logic xorAB, Logic sum, Logic cOut)
faOps,
) : super(name: 'full_adder') {
a = addInput('a', a);
b = addInput('b', b);
cIn = addInput('c_in', cIn);
final sum = addOutput('sum');
final cOut = addOutput('c_out');
final xorAB = Logic(name: 'xor_ab');
faOps(a, b, cIn, xorAB, sum, cOut);
}
}
class FAResult {
int sum = 0;
int cOut = 0;
}
FAResult faTruthTable(int a, int b, int cIn) {
final res = FAResult();
if (a + b + cIn == 0) {
return res
..sum = 0
..cOut = 0;
} else if (a + b + cIn == 3) {
return res
..sum = 0 // the correct answer should be 1
..cOut = 1;
} else if (a + b + cIn == 1) {
return res
..sum = 1
..cOut = 0;
} else {
return res
..sum = 0
..cOut = 1;
}
}
void faOps(Logic a, Logic b, Logic cIn, Logic xorAB, Logic sum, Logic cOut) {
// SUM
xorAB <= a ^ b;
sum <= xorAB ^ cIn;
// C-Out
cOut <= xorAB & cIn | a & b;
}
void main() async {
final a = Logic(name: 'a');
final b = Logic(name: 'b');
final cIn = Logic(name: 'c_in');
final xorAB = Logic(name: 'xor_ab');
final sum = Logic(name: 'sum');
final cOut = Logic(name: 'cOut');
faOps(a, b, cIn, xorAB, sum, cOut);
test('should return true if result sum similar to truth table.', () async {
for (var i = 0; i <= 1; i++) {
for (var j = 0; j <= 1; j++) {
for (var k = 0; k <= 1; k++) {
a.put(i);
b.put(j);
cIn.put(k);
expect(sum.value.toInt(), faTruthTable(i, j, k).sum);
}
}
}
});
final mod = FullAdderModule(a, b, cIn, faOps);
await mod.build();
} Current test failure output: 02:38 +0 -1: should return true if result sum similar to truth table. [E]
Expected: <0>
Actual: <1>
package:test_api/src/expect/expect.dart 134:31 fail
package:test_api/src/expect/expect.dart 129:3 _expect
package:test_api/src/expect/expect.dart 46:3 expect
doc/tutorials/chapter_3/test.dart 81:11 main.<fn>
package:test_api/src/backend/declarer.dart 215:19 Declarer.test.<fn>.<fn>
===== asynchronous gap ===========================
package:test_api/src/backend/declarer.dart 213:7 Declarer.test.<fn>
===== asynchronous gap ===========================
package:test_api/src/backend/invoker.dart 257:7 Invoker._waitForOutstandingCallbacks.<fn>
02:38 +0 -1: Some tests failed. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
From what I can think of is using setUpAll(() {
a.put(0);
b.put(0);
cIn.put(0);
});
tearDownAll(() {
a.put(0);
b.put(0);
cIn.put(0);
});
test('should return true if result sum similar to truth table.', () async {
for (var i = 0; i <= 1; i++) {
for (var j = 0; j <= 1; j++) {
for (var k = 0; k <= 1; k++) {
a.put(i);
b.put(j);
cIn.put(k);
try {
expect(sum.value.toInt(), faTruthTable(i, j, k).sum);
} on Exception {
print('a: ${a.value.toInt()}');
print('b: ${b.value.toInt()}');
print('cIn: ${cIn.value.toInt()}');
rethrow;
}
}
}
}
}); |
Beta Was this translation helpful? Give feedback.
-
You can use the For example: final expected = faTruthTable(i, j, k).sum;
final actual = sum.value.toInt();
expect(sum.value.toInt(), expected,
reason: 'For inputs a=$i, b=$j, cIn=$k,'
' expected sum=$expected but found $actual'); Now when the test fails it prints
|
Beta Was this translation helpful? Give feedback.
You can use the
reason
named argument inexpect
to print a more helpful message.For example:
Now when the test fails it prints