Add unit tests for wxDecToHex()

This commit is contained in:
Artur Wieczorek
2017-02-23 20:01:48 +01:00
parent 2934f77660
commit 2ecd4a07ba
11 changed files with 136 additions and 0 deletions

View File

@@ -115,6 +115,7 @@ TEST_OBJECTS = \
test_vararg.o \ test_vararg.o \
test_crt.o \ test_crt.o \
test_vsnprintf.o \ test_vsnprintf.o \
test_hexconv.o \
test_bstream.o \ test_bstream.o \
test_datastreamtest.o \ test_datastreamtest.o \
test_ffilestream.o \ test_ffilestream.o \
@@ -681,6 +682,9 @@ test_crt.o: $(srcdir)/strings/crt.cpp $(TEST_ODEP)
test_vsnprintf.o: $(srcdir)/strings/vsnprintf.cpp $(TEST_ODEP) test_vsnprintf.o: $(srcdir)/strings/vsnprintf.cpp $(TEST_ODEP)
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/strings/vsnprintf.cpp $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/strings/vsnprintf.cpp
test_hexconv.o: $(srcdir)/strings/hexconv.cpp $(TEST_ODEP)
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/strings/hexconv.cpp
test_bstream.o: $(srcdir)/streams/bstream.cpp $(TEST_ODEP) test_bstream.o: $(srcdir)/streams/bstream.cpp $(TEST_ODEP)
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/streams/bstream.cpp $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/streams/bstream.cpp

View File

@@ -95,6 +95,7 @@ TEST_OBJECTS = \
$(OBJS)\test_vararg.obj \ $(OBJS)\test_vararg.obj \
$(OBJS)\test_crt.obj \ $(OBJS)\test_crt.obj \
$(OBJS)\test_vsnprintf.obj \ $(OBJS)\test_vsnprintf.obj \
$(OBJS)\test_hexconv.obj \
$(OBJS)\test_bstream.obj \ $(OBJS)\test_bstream.obj \
$(OBJS)\test_datastreamtest.obj \ $(OBJS)\test_datastreamtest.obj \
$(OBJS)\test_ffilestream.obj \ $(OBJS)\test_ffilestream.obj \
@@ -711,6 +712,9 @@ $(OBJS)\test_crt.obj: .\strings\crt.cpp
$(OBJS)\test_vsnprintf.obj: .\strings\vsnprintf.cpp $(OBJS)\test_vsnprintf.obj: .\strings\vsnprintf.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\strings\vsnprintf.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\strings\vsnprintf.cpp
$(OBJS)\test_hexconv.obj: .\strings\hexconv.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\strings\hexconv.cpp
$(OBJS)\test_bstream.obj: .\streams\bstream.cpp $(OBJS)\test_bstream.obj: .\streams\bstream.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\streams\bstream.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\streams\bstream.cpp

View File

@@ -87,6 +87,7 @@ TEST_OBJECTS = \
$(OBJS)\test_vararg.o \ $(OBJS)\test_vararg.o \
$(OBJS)\test_crt.o \ $(OBJS)\test_crt.o \
$(OBJS)\test_vsnprintf.o \ $(OBJS)\test_vsnprintf.o \
$(OBJS)\test_hexconv.o \
$(OBJS)\test_bstream.o \ $(OBJS)\test_bstream.o \
$(OBJS)\test_datastreamtest.o \ $(OBJS)\test_datastreamtest.o \
$(OBJS)\test_ffilestream.o \ $(OBJS)\test_ffilestream.o \
@@ -687,6 +688,9 @@ $(OBJS)\test_crt.o: ./strings/crt.cpp
$(OBJS)\test_vsnprintf.o: ./strings/vsnprintf.cpp $(OBJS)\test_vsnprintf.o: ./strings/vsnprintf.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_hexconv.o: ./strings/hexconv.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_bstream.o: ./streams/bstream.cpp $(OBJS)\test_bstream.o: ./streams/bstream.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<

View File

@@ -89,6 +89,7 @@ TEST_OBJECTS = \
$(OBJS)\test_vararg.obj \ $(OBJS)\test_vararg.obj \
$(OBJS)\test_crt.obj \ $(OBJS)\test_crt.obj \
$(OBJS)\test_vsnprintf.obj \ $(OBJS)\test_vsnprintf.obj \
$(OBJS)\test_hexconv.obj \
$(OBJS)\test_bstream.obj \ $(OBJS)\test_bstream.obj \
$(OBJS)\test_datastreamtest.obj \ $(OBJS)\test_datastreamtest.obj \
$(OBJS)\test_ffilestream.obj \ $(OBJS)\test_ffilestream.obj \
@@ -888,6 +889,9 @@ $(OBJS)\test_crt.obj: .\strings\crt.cpp
$(OBJS)\test_vsnprintf.obj: .\strings\vsnprintf.cpp $(OBJS)\test_vsnprintf.obj: .\strings\vsnprintf.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\strings\vsnprintf.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\strings\vsnprintf.cpp
$(OBJS)\test_hexconv.obj: .\strings\hexconv.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\strings\hexconv.cpp
$(OBJS)\test_bstream.obj: .\streams\bstream.cpp $(OBJS)\test_bstream.obj: .\streams\bstream.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\streams\bstream.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\streams\bstream.cpp

104
tests/strings/hexconv.cpp Normal file
View File

@@ -0,0 +1,104 @@
///////////////////////////////////////////////////////////////////////////////
// Name: tests/strings/hexconv.cpp
// Purpose: wxDecToHex unit test
// Author: Artur Wieczorek
// Created: 2017-02-23
// Copyright: (c) 2017 wxWidgets development team
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "wx/utils.h"
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class HexConvTestCase : public CppUnit::TestCase
{
public:
HexConvTestCase() {}
private:
CPPUNIT_TEST_SUITE( HexConvTestCase );
CPPUNIT_TEST( DecToHex1 ); // Conversion to wxString
CPPUNIT_TEST( DecToHex2 ); // Conversion to wxChar string
CPPUNIT_TEST( DecToHex3 ); // Conversion to 2 characters
CPPUNIT_TEST_SUITE_END();
void DecToHex1();
void DecToHex2();
void DecToHex3();
wxDECLARE_NO_COPY_CLASS(HexConvTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( HexConvTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( HexConvTestCase, "HexConvTestCase" );
// ----------------------------------------------------------------------------
// tests themselves
// ----------------------------------------------------------------------------
void HexConvTestCase::DecToHex1()
{
// Conversion to wxString
for ( int i = 0; i < 256; i++ )
{
char szHexStrRef[16];
sprintf(szHexStrRef, "%02X", i);
wxString hexStrRef = wxString(szHexStrRef);
wxString hexStr = wxDecToHex(i);
CPPUNIT_ASSERT_EQUAL( hexStr, hexStrRef );
}
}
void HexConvTestCase::DecToHex2()
{
// Conversion to wxChar string
for ( int i = 0; i < 256; i++ )
{
char szHexStrRef[16];
sprintf(szHexStrRef, "%02X", i);
wxChar hexStr[4];
memset(hexStr, 0xFF, sizeof(hexStr));
wxChar c3 = hexStr[3]; // This character should remain untouched
wxDecToHex(i, hexStr);
CPPUNIT_ASSERT_EQUAL( hexStr[0], (wxChar)szHexStrRef[0] );
CPPUNIT_ASSERT_EQUAL( hexStr[1], (wxChar)szHexStrRef[1] );
CPPUNIT_ASSERT_EQUAL( hexStr[2], wxS('\0') );
CPPUNIT_ASSERT_EQUAL( hexStr[3], c3 );
}
}
void HexConvTestCase::DecToHex3()
{
// Conversion to 2 characters
for ( int i = 0; i < 256; i++ )
{
char szHexStrRef[16];
sprintf(szHexStrRef, "%02X", i);
char c1 = '\xFF';
char c2 = '\xFF';
wxDecToHex(i, &c1, &c2);
CPPUNIT_ASSERT_EQUAL( c1, szHexStrRef[0] );
CPPUNIT_ASSERT_EQUAL( c2, szHexStrRef[1] );
}
}

View File

@@ -84,6 +84,7 @@
strings/vararg.cpp strings/vararg.cpp
strings/crt.cpp strings/crt.cpp
strings/vsnprintf.cpp strings/vsnprintf.cpp
strings/hexconv.cpp
streams/bstream.cpp streams/bstream.cpp
streams/datastreamtest.cpp streams/datastreamtest.cpp
streams/ffilestream.cpp streams/ffilestream.cpp

View File

@@ -535,6 +535,7 @@
<ClCompile Include="strings\unicode.cpp" /> <ClCompile Include="strings\unicode.cpp" />
<ClCompile Include="strings\vararg.cpp" /> <ClCompile Include="strings\vararg.cpp" />
<ClCompile Include="strings\vsnprintf.cpp" /> <ClCompile Include="strings\vsnprintf.cpp" />
<ClCompile Include="strings\hexconv.cpp" />
<ClCompile Include="test.cpp" /> <ClCompile Include="test.cpp" />
<ClCompile Include="textfile\textfiletest.cpp" /> <ClCompile Include="textfile\textfiletest.cpp" />
<ClCompile Include="thread\atomic.cpp" /> <ClCompile Include="thread\atomic.cpp" />

View File

@@ -244,6 +244,9 @@
<ClCompile Include="strings\vsnprintf.cpp"> <ClCompile Include="strings\vsnprintf.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="strings\hexconv.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="weakref\weakref.cpp"> <ClCompile Include="weakref\weakref.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>

View File

@@ -556,6 +556,9 @@
<File <File
RelativePath=".\strings\vsnprintf.cpp"> RelativePath=".\strings\vsnprintf.cpp">
</File> </File>
<File
RelativePath=".\strings\hexconv.cpp">
</File>
<File <File
RelativePath=".\weakref\weakref.cpp"> RelativePath=".\weakref\weakref.cpp">
</File> </File>

View File

@@ -1210,6 +1210,10 @@
RelativePath=".\strings\vsnprintf.cpp" RelativePath=".\strings\vsnprintf.cpp"
> >
</File> </File>
<File
RelativePath=".\strings\hexconv.cpp"
>
</File>
<File <File
RelativePath=".\weakref\weakref.cpp" RelativePath=".\weakref\weakref.cpp"
> >

View File

@@ -1182,6 +1182,10 @@
RelativePath=".\strings\vsnprintf.cpp" RelativePath=".\strings\vsnprintf.cpp"
> >
</File> </File>
<File
RelativePath=".\strings\hexconv.cpp"
>
</File>
<File <File
RelativePath=".\weakref\weakref.cpp" RelativePath=".\weakref\weakref.cpp"
> >