Commit 6bbfc7e8 authored by Davis King's avatar Davis King

Changed the crc32 object so you don't have to say crc32::kernel_1a anymore to

declare it.  You just say crc32.  I also added some unit tests for it as well
as a few minor convenience functions.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404215
parent 93013870
...@@ -6,30 +6,5 @@ ...@@ -6,30 +6,5 @@
#include "crc32/crc32_kernel_1.h" #include "crc32/crc32_kernel_1.h"
namespace dlib
{
class crc32
{
crc32() {}
public:
//----------- kernels ---------------
// kernel_1a
typedef crc32_kernel_1
kernel_1a;
};
}
#endif // DLIB_CRc32_ #endif // DLIB_CRc32_
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace dlib namespace dlib
{ {
class crc32_kernel_1 class crc32
{ {
/*! /*!
INITIAL VALUE INITIAL VALUE
...@@ -24,10 +24,17 @@ namespace dlib ...@@ -24,10 +24,17 @@ namespace dlib
public: public:
inline crc32_kernel_1 ( // this is here for backwards compatibility with older versions of dlib.
typedef crc32 kernel_1a;
inline crc32 (
);
inline crc32 (
const std::string& item
); );
inline virtual ~crc32_kernel_1 ( inline virtual ~crc32 (
); );
inline void clear( inline void clear(
...@@ -45,23 +52,27 @@ namespace dlib ...@@ -45,23 +52,27 @@ namespace dlib
) const; ) const;
inline void swap ( inline void swap (
crc32_kernel_1& item crc32& item
); );
inline crc32& operator=(
const crc32&
);
private: private:
inline void fill_crc_table(
);
unsigned long checksum; unsigned long checksum;
unsigned long table[256]; unsigned long table[256];
// restricted functions
crc32_kernel_1(const crc32_kernel_1&); // copy constructor
crc32_kernel_1& operator=(const crc32_kernel_1&); // assignment operator
}; };
inline void swap ( inline void swap (
crc32_kernel_1& a, crc32& a,
crc32_kernel_1& b crc32& b
) { a.swap(b); } ) { a.swap(b); }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -70,11 +81,10 @@ namespace dlib ...@@ -70,11 +81,10 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
crc32_kernel_1:: void crc32::
crc32_kernel_1 ( fill_crc_table (
) )
{ {
checksum = 0xFFFFFFFF;
unsigned long temp; unsigned long temp;
// fill out the crc table // fill out the crc table
...@@ -90,20 +100,41 @@ namespace dlib ...@@ -90,20 +100,41 @@ namespace dlib
} }
table[i] = temp; table[i] = temp;
} }
}
// ----------------------------------------------------------------------------------------
crc32::
crc32 (
)
{
checksum = 0xFFFFFFFF;
fill_crc_table();
}
// ----------------------------------------------------------------------------------------
crc32::
crc32 (
const std::string& item
)
{
checksum = 0xFFFFFFFF;
fill_crc_table();
add(item);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
crc32_kernel_1:: crc32::
~crc32_kernel_1 ( ~crc32 (
) )
{ {
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void crc32_kernel_1:: void crc32::
clear( clear(
) )
{ {
...@@ -112,7 +143,7 @@ namespace dlib ...@@ -112,7 +143,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void crc32_kernel_1:: void crc32::
add ( add (
unsigned char item unsigned char item
) )
...@@ -122,7 +153,7 @@ namespace dlib ...@@ -122,7 +153,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void crc32_kernel_1:: void crc32::
add ( add (
const std::string& item const std::string& item
) )
...@@ -133,7 +164,7 @@ namespace dlib ...@@ -133,7 +164,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
unsigned long crc32_kernel_1:: unsigned long crc32::
get_checksum ( get_checksum (
) const ) const
{ {
...@@ -142,14 +173,25 @@ namespace dlib ...@@ -142,14 +173,25 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void crc32_kernel_1:: void crc32::
swap ( swap (
crc32_kernel_1& item crc32& item
) )
{ {
exchange(checksum,item.checksum); exchange(checksum,item.checksum);
} }
// ----------------------------------------------------------------------------------------
crc32& crc32::
operator=(
const crc32& item
)
{
checksum = item.checksum;
return *this;
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
...@@ -28,8 +28,17 @@ namespace dlib ...@@ -28,8 +28,17 @@ namespace dlib
/*! /*!
ensures ensures
- #*this is properly initialized - #*this is properly initialized
throws !*/
- std::bad_alloc
crc32 (
const std::string& item
);
/*!
ensures
- #*this is properly initialized
- calls this->add(item).
(i.e. Using this constructor is the same as using the default
constructor and then calling add() on item)
!*/ !*/
virtual ~crc32 ( virtual ~crc32 (
...@@ -44,10 +53,6 @@ namespace dlib ...@@ -44,10 +53,6 @@ namespace dlib
/*! /*!
ensures ensures
- #*this has its initial value - #*this has its initial value
throws
- std::bad_alloc
if this exception is thrown then #*this is unusable
until clear() is called and succeeds
!*/ !*/
void add ( void add (
...@@ -83,12 +88,6 @@ namespace dlib ...@@ -83,12 +88,6 @@ namespace dlib
- swaps *this and item - swaps *this and item
!*/ !*/
private:
// restricted functions
crc32(const crc32&); // copy constructor
crc32& operator=(const crc32&); // assignment operator
}; };
void swap ( void swap (
......
...@@ -32,6 +32,7 @@ set (tests ...@@ -32,6 +32,7 @@ set (tests
conditioning_class_c.cpp conditioning_class_c.cpp
conditioning_class.cpp conditioning_class.cpp
config_reader.cpp config_reader.cpp
crc32.cpp
create_iris_datafile.cpp create_iris_datafile.cpp
data_io.cpp data_io.cpp
directed_graph.cpp directed_graph.cpp
......
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <dlib/crc32.h>
#include "tester.h"
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
logger dlog("test.crc32");
class crc32_tester : public tester
{
public:
crc32_tester (
) :
tester ("test_crc32",
"Runs tests on the crc32 component.")
{}
void perform_test (
)
{
DLIB_TEST(crc32("davis").get_checksum() == 0x0445527C);
crc32 c, c2;
DLIB_TEST(c.get_checksum() == 0);
c.add("davis");
DLIB_TEST(c.get_checksum() == 0x0445527C);
DLIB_TEST(c2.get_checksum() == 0);
c2 = c;
DLIB_TEST(c2.get_checksum() == 0x0445527C);
crc32 c3(c);
DLIB_TEST(c3.get_checksum() == 0x0445527C);
c.add('a');
c2.add('a');
c3.add('a');
DLIB_TEST(c.get_checksum() == 0xB100C606);
DLIB_TEST(c2.get_checksum() == 0xB100C606);
DLIB_TEST(c3.get_checksum() == 0xB100C606);
crc32::kernel_1a cold;
DLIB_TEST(cold.get_checksum() == 0);
cold.add("davis");
DLIB_TEST(cold.get_checksum() == 0x0445527C);
c.clear();
DLIB_TEST(c.get_checksum() == 0);
c.add("davis");
DLIB_TEST(c.get_checksum() == 0x0445527C);
}
} a;
}
...@@ -42,6 +42,7 @@ SRC += compress_stream.cpp ...@@ -42,6 +42,7 @@ SRC += compress_stream.cpp
SRC += conditioning_class_c.cpp SRC += conditioning_class_c.cpp
SRC += conditioning_class.cpp SRC += conditioning_class.cpp
SRC += config_reader.cpp SRC += config_reader.cpp
SRC += crc32.cpp
SRC += create_iris_datafile.cpp SRC += create_iris_datafile.cpp
SRC += data_io.cpp SRC += data_io.cpp
SRC += directed_graph.cpp SRC += directed_graph.cpp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment