Initial commit, version 0.1.0.
[libfsafe.git] / tests / Example.cxx
blobbd1de1f5e725da26ed0da34e0f8e496ae4a13369
1 #include <tests/Example.hxx>
2 #include <libsex/Exception.hxx>
3 #include <libfsafe/checkpoint.hxx>
4 #include <libfsafe/assert.hxx>
5 #include <sstream>
7 // main() is the entry point -- both for our example
8 // "application" and this unit test.
10 void tests::Example::main()
12 std::stringstream cout;
14 try {
15 // Real work starts now.
17 initialize();
18 run();
19 cleanup();
21 // Real work finished.
23 } catch (libsex::Exception& e) {
24 e.backtrace(cout);
27 std::string exp =
28 __FILE__ ":54: In 'c = calculate(a)':\n"
29 __FILE__ ":64: Precondition 'ptr[1] < 42' violated.";
30 CPPUNIT_ASSERT_EQUAL(exp, cout.str());
33 void tests::Example::initialize()
35 // Some boring initialization stuff.
38 void tests::Example::cleanup()
40 // Boring cleanup here.
43 void tests::Example::run()
45 // The really important part of our code.
47 int a[2];
48 a[0] = 41;
49 a[1] = 42;
51 // Note that we can't declare c in CHECKPOINT
52 // because it would be wrapped in try-catch.
53 int c;
54 CHECKPOINT(c = calculate(a));
56 // Now do something with c.
57 (void) c;
60 int tests::Example::calculate(int* ptr)
62 ASSERT_PRE(ptr != NULL);
63 ASSERT_PRE(ptr[0] < 42);
64 ASSERT_PRE(ptr[1] < 42);
66 int result = ptr[0] + ptr[1];
68 ASSERT_POST(result <= 82);
69 return result;