From 2ecd4a07ba384d1a08611a480d8283bf315b53be Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Thu, 23 Feb 2017 20:01:48 +0100 Subject: [PATCH] Add unit tests for wxDecToHex() --- tests/Makefile.in | 4 ++ tests/makefile.bcc | 4 ++ tests/makefile.gcc | 4 ++ tests/makefile.vc | 4 ++ tests/strings/hexconv.cpp | 104 +++++++++++++++++++++++++++++++++++++ tests/test.bkl | 1 + tests/test.vcxproj | 1 + tests/test.vcxproj.filters | 3 ++ tests/test_vc7_test.vcproj | 3 ++ tests/test_vc8_test.vcproj | 4 ++ tests/test_vc9_test.vcproj | 4 ++ 11 files changed, 136 insertions(+) create mode 100644 tests/strings/hexconv.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index 2f11c9fd7b..4dfb9c00f4 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -115,6 +115,7 @@ TEST_OBJECTS = \ test_vararg.o \ test_crt.o \ test_vsnprintf.o \ + test_hexconv.o \ test_bstream.o \ test_datastreamtest.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) $(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) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/streams/bstream.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 1082200e75..5260e37a74 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -95,6 +95,7 @@ TEST_OBJECTS = \ $(OBJS)\test_vararg.obj \ $(OBJS)\test_crt.obj \ $(OBJS)\test_vsnprintf.obj \ + $(OBJS)\test_hexconv.obj \ $(OBJS)\test_bstream.obj \ $(OBJS)\test_datastreamtest.obj \ $(OBJS)\test_ffilestream.obj \ @@ -711,6 +712,9 @@ $(OBJS)\test_crt.obj: .\strings\crt.cpp $(OBJS)\test_vsnprintf.obj: .\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 $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\streams\bstream.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 4edcf1a858..a196aba3d9 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -87,6 +87,7 @@ TEST_OBJECTS = \ $(OBJS)\test_vararg.o \ $(OBJS)\test_crt.o \ $(OBJS)\test_vsnprintf.o \ + $(OBJS)\test_hexconv.o \ $(OBJS)\test_bstream.o \ $(OBJS)\test_datastreamtest.o \ $(OBJS)\test_ffilestream.o \ @@ -687,6 +688,9 @@ $(OBJS)\test_crt.o: ./strings/crt.cpp $(OBJS)\test_vsnprintf.o: ./strings/vsnprintf.cpp $(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 $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index 26cc96d092..7763406919 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -89,6 +89,7 @@ TEST_OBJECTS = \ $(OBJS)\test_vararg.obj \ $(OBJS)\test_crt.obj \ $(OBJS)\test_vsnprintf.obj \ + $(OBJS)\test_hexconv.obj \ $(OBJS)\test_bstream.obj \ $(OBJS)\test_datastreamtest.obj \ $(OBJS)\test_ffilestream.obj \ @@ -888,6 +889,9 @@ $(OBJS)\test_crt.obj: .\strings\crt.cpp $(OBJS)\test_vsnprintf.obj: .\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 $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\streams\bstream.cpp diff --git a/tests/strings/hexconv.cpp b/tests/strings/hexconv.cpp new file mode 100644 index 0000000000..c0cdfde77a --- /dev/null +++ b/tests/strings/hexconv.cpp @@ -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] ); + } +} diff --git a/tests/test.bkl b/tests/test.bkl index d913bd9e5c..2d0fcae002 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -84,6 +84,7 @@ strings/vararg.cpp strings/crt.cpp strings/vsnprintf.cpp + strings/hexconv.cpp streams/bstream.cpp streams/datastreamtest.cpp streams/ffilestream.cpp diff --git a/tests/test.vcxproj b/tests/test.vcxproj index ce42ec211d..feb395234b 100644 --- a/tests/test.vcxproj +++ b/tests/test.vcxproj @@ -535,6 +535,7 @@ + diff --git a/tests/test.vcxproj.filters b/tests/test.vcxproj.filters index cf2067ebf2..fc68871406 100644 --- a/tests/test.vcxproj.filters +++ b/tests/test.vcxproj.filters @@ -244,6 +244,9 @@ Source Files + + Source Files + Source Files diff --git a/tests/test_vc7_test.vcproj b/tests/test_vc7_test.vcproj index ec9d377ee9..ee6e23ee6e 100644 --- a/tests/test_vc7_test.vcproj +++ b/tests/test_vc7_test.vcproj @@ -556,6 +556,9 @@ + + diff --git a/tests/test_vc8_test.vcproj b/tests/test_vc8_test.vcproj index b2014c9194..d403c906f5 100644 --- a/tests/test_vc8_test.vcproj +++ b/tests/test_vc8_test.vcproj @@ -1210,6 +1210,10 @@ RelativePath=".\strings\vsnprintf.cpp" > + + diff --git a/tests/test_vc9_test.vcproj b/tests/test_vc9_test.vcproj index 32f9ab3643..95ed019c60 100644 --- a/tests/test_vc9_test.vcproj +++ b/tests/test_vc9_test.vcproj @@ -1182,6 +1182,10 @@ RelativePath=".\strings\vsnprintf.cpp" > + +