annotate C++/Tests/Pack/TestPack.cpp @ 223:c6513d9c696b

Pack: add unit tests
author David Demelier <markand@malikania.fr>
date Thu, 08 May 2014 23:51:07 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
223
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * TestPack.cpp -- test the pack serializer
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <cppunit/TextTestRunner.h>
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
20
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <Pack.h>
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
22
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include "TestPack.h"
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
24
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 void TestPack::simpleLittleEndian()
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
26 {
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 uint8_t u8(1), r8;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 uint16_t u16(2), r16;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 uint32_t u32(3), r32;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 uint64_t u64(4), r64;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
31
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 try {
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 Pack::write("simple-little.bin", Pack::Little, u8, u16, u32, u64);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 Pack::read("simple-little.bin", Pack::Little, r8, r16, r32, r64);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
35
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 CPPUNIT_ASSERT_EQUAL(r8, u8);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 CPPUNIT_ASSERT_EQUAL(r16, u16);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 CPPUNIT_ASSERT_EQUAL(r32, u32);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 CPPUNIT_ASSERT_EQUAL(r64, u64);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 } catch (const std::runtime_error &) { }
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 }
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
42
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
43 void TestPack::simpleBigEndian()
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 {
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 uint8_t u8(1), r8;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 uint16_t u16(2), r16;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 uint32_t u32(3), r32;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 uint64_t u64(4), r64;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
49
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 try {
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 Pack::write("simple-big.bin", Pack::Big, u8, u16, u32, u64);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 Pack::read("simple-big.bin", Pack::Big, r8, r16, r32, r64);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
53
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 CPPUNIT_ASSERT_EQUAL(r8, u8);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 CPPUNIT_ASSERT_EQUAL(r16, u16);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 CPPUNIT_ASSERT_EQUAL(r32, u32);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 CPPUNIT_ASSERT_EQUAL(r64, u64);
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 } catch (const std::runtime_error &) { }
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 }
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
60
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
61 int main()
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 {
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 CppUnit::TextTestRunner runnerText;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
64
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 runnerText.addTest(TestPack::suite());
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
66
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 return runnerText.run("", false) == 1 ? 0 : 1;
c6513d9c696b Pack: add unit tests
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 }