Engauge Digitizer 2
Loading...
Searching...
No Matches
TestCrc32.cpp
Go to the documentation of this file.
1#include "Crc32.h"
2#include "Logger.h"
3#include <qmath.h>
4#include <QTemporaryFile>
5#include <QtTest/QtTest>
6#include "Test/TestCrc32.h"
7
8QTEST_MAIN (TestCrc32)
9
10unsigned char inBuffer[] = {"abcdef"};
11unsigned outExpectedBinary = 3323750366; // From cksum of file containing inBuffer contents without carriage return
12unsigned outExpectedText = 773139377; // From cksum of file containing inBuffer contents without carriage return
13
14TestCrc32::TestCrc32(QObject *parent) :
15 QObject(parent)
16{
17}
18
19void TestCrc32::cleanupTestCase ()
20{
21}
22
23void TestCrc32::initTestCase ()
24{
25 const bool DEBUG_FLAG = false;
26
27 initializeLogging ("engauge_test",
28 "engauge_test.log",
30}
31
32void TestCrc32::testFileBinary ()
33{
34 unsigned outGot = 0;
35
36 QTemporaryFile temp;
37 temp.setAutoRemove (false);
38 QString filename;
39 if (temp.open ()) {
40
41 // Save file name for reading after writing, closing then calculating
42 filename = temp.fileName ();
43
44 int length = sizeof (inBuffer) / sizeof (unsigned char) - 1;
45
46 QDataStream str (&temp);
47 str.writeBytes ((const char*) inBuffer, length);
48
49 temp.close ();
50
51 // Calculate
52 Crc32 crc32;
53 outGot = crc32.filecrc (filename);
54
55 QFile::remove (filename);
56 }
57
58 QVERIFY (outGot == outExpectedBinary);
59}
60
61void TestCrc32::testFileText ()
62{
63 unsigned outGot = 0;
64
65 QTemporaryFile temp;
66 temp.setAutoRemove (false);
67 QString filename;
68 if (temp.open ()) {
69
70 // Save file name for reading after writing, closing then calculating
71 filename = temp.fileName ();
72
73 QTextStream str (&temp);
74 str << QString ((const char*) inBuffer);
75
76 temp.close ();
77
78 // Calculate
79 Crc32 crc32;
80 outGot = crc32.filecrc (filename);
81
82 QFile::remove (filename);
83 }
84
85 QVERIFY (outGot == outExpectedText);
86}
87
88void TestCrc32::testMemory ()
89{
90 Crc32 crc32;
91
92 int length = sizeof (inBuffer) / sizeof (unsigned char) - 1;
93
94 unsigned outGot = crc32.memcrc (inBuffer,
95 length);
96
97 QVERIFY (outGot == outExpectedText);
98}
void initializeLogging(const QString &name, const QString &filename, bool isDebug)
Definition Logger.cpp:21
unsigned outExpectedBinary
Definition TestCrc32.cpp:11
unsigned outExpectedText
Definition TestCrc32.cpp:12
unsigned char inBuffer[]
Definition TestCrc32.cpp:10
const bool DEBUG_FLAG
unsigned memcrc(const unsigned char *b, unsigned int n) const
Compute the checksum using data in memory.
Definition Crc32.cpp:100
unsigned filecrc(const QString &filename) const
Compute the checksum using data in file.
Definition Crc32.cpp:69
Unit tests of crc32 checksum algorithm.
Definition TestCrc32.h:8
TestCrc32(QObject *parent=0)
Single constructor.
Definition TestCrc32.cpp:14