comparison UnitTestsSources/UnitTestsMain.cpp @ 1939:d80a4fe8ffcc

tests EndiannessConversions32/64
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Mar 2016 10:48:12 +0100
parents 48ad54f7b21f
children 5514d37176b4
comparison
equal deleted inserted replaced
1938:48ad54f7b21f 1939:d80a4fe8ffcc
689 689
690 #include "../Core/Endianness.h" 690 #include "../Core/Endianness.h"
691 691
692 static void ASSERT_EQ16(uint16_t a, uint16_t b) 692 static void ASSERT_EQ16(uint16_t a, uint16_t b)
693 { 693 {
694 #ifdef __MINGW32__
694 // This cast solves a linking problem with MinGW 695 // This cast solves a linking problem with MinGW
695 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); 696 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
696 } 697 #else
697 698 ASSERT_EQ(a, b);
699 #endif
700 }
698 701
699 static void ASSERT_NE16(uint16_t a, uint16_t b) 702 static void ASSERT_NE16(uint16_t a, uint16_t b)
700 { 703 {
704 #ifdef __MINGW32__
701 // This cast solves a linking problem with MinGW 705 // This cast solves a linking problem with MinGW
702 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); 706 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
703 } 707 #else
708 ASSERT_NE(a, b);
709 #endif
710 }
711
712 static void ASSERT_EQ32(uint32_t a, uint32_t b)
713 {
714 #ifdef __MINGW32__
715 // This cast solves a linking problem with MinGW
716 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
717 #else
718 ASSERT_EQ(a, b);
719 #endif
720 }
721
722 static void ASSERT_NE32(uint32_t a, uint32_t b)
723 {
724 #ifdef __MINGW32__
725 // This cast solves a linking problem with MinGW
726 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
727 #else
728 ASSERT_NE(a, b);
729 #endif
730 }
731
732 static void ASSERT_EQ64(uint64_t a, uint64_t b)
733 {
734 #ifdef __MINGW32__
735 // This cast solves a linking problem with MinGW
736 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
737 #else
738 ASSERT_EQ(a, b);
739 #endif
740 }
741
742 static void ASSERT_NE64(uint64_t a, uint64_t b)
743 {
744 #ifdef __MINGW32__
745 // This cast solves a linking problem with MinGW
746 ASSERT_NE(static_cast<unsigned long long>(a), static_cast<unsigned long long>(b));
747 #else
748 ASSERT_NE(a, b);
749 #endif
750 }
751
704 752
705 753
706 TEST(Toolbox, EndiannessConversions16) 754 TEST(Toolbox, EndiannessConversions16)
707 { 755 {
708 Endianness e = Toolbox::DetectEndianness(); 756 Endianness e = Toolbox::DetectEndianness();
709 757
710 for (unsigned int i = 0; i < 65536; i++) 758 for (unsigned int i = 0; i < 65536; i += 17)
711 { 759 {
712 uint16_t v = static_cast<uint16_t>(i); 760 uint16_t v = static_cast<uint16_t>(i);
713 ASSERT_EQ16(v, be16toh(htobe16(v))); 761 ASSERT_EQ16(v, be16toh(htobe16(v)));
714 ASSERT_EQ16(v, le16toh(htole16(v))); 762 ASSERT_EQ16(v, le16toh(htole16(v)));
715 763
756 } 804 }
757 } 805 }
758 } 806 }
759 807
760 808
809 TEST(Toolbox, EndiannessConversions32)
810 {
811 const uint32_t v = 0xff010203u;
812 const uint32_t r = 0x030201ffu;
813 ASSERT_EQ32(v, be32toh(htobe32(v)));
814 ASSERT_EQ32(v, le32toh(htole32(v)));
815 ASSERT_NE32(v, be32toh(htole32(v)));
816 ASSERT_NE32(v, le32toh(htobe32(v)));
817
818 switch (Toolbox::DetectEndianness())
819 {
820 case Endianness_Little:
821 ASSERT_EQ32(r, htobe32(v));
822 ASSERT_EQ32(v, htole32(v));
823 ASSERT_EQ32(r, be32toh(v));
824 ASSERT_EQ32(v, le32toh(v));
825 break;
826
827 case Endianness_Big:
828 ASSERT_EQ32(v, htobe32(v));
829 ASSERT_EQ32(r, htole32(v));
830 ASSERT_EQ32(v, be32toh(v));
831 ASSERT_EQ32(r, le32toh(v));
832 break;
833
834 default:
835 throw OrthancException(ErrorCode_ParameterOutOfRange);
836 }
837 }
838
839
840 TEST(Toolbox, EndiannessConversions64)
841 {
842 const uint64_t v = 0xff01020304050607LLu;
843 const uint64_t r = 0x07060504030201ffLLu;
844 ASSERT_EQ64(v, be64toh(htobe64(v)));
845 ASSERT_EQ64(v, le64toh(htole64(v)));
846 ASSERT_NE64(v, be64toh(htole64(v)));
847 ASSERT_NE64(v, le64toh(htobe64(v)));
848
849 switch (Toolbox::DetectEndianness())
850 {
851 case Endianness_Little:
852 ASSERT_EQ64(r, htobe64(v));
853 ASSERT_EQ64(v, htole64(v));
854 ASSERT_EQ64(r, be64toh(v));
855 ASSERT_EQ64(v, le64toh(v));
856 break;
857
858 case Endianness_Big:
859 ASSERT_EQ64(v, htobe64(v));
860 ASSERT_EQ64(r, htole64(v));
861 ASSERT_EQ64(v, be64toh(v));
862 ASSERT_EQ64(r, le64toh(v));
863 break;
864
865 default:
866 throw OrthancException(ErrorCode_ParameterOutOfRange);
867 }
868 }
761 869
762 870
763 871
764 #if ORTHANC_PUGIXML_ENABLED == 1 872 #if ORTHANC_PUGIXML_ENABLED == 1
765 TEST(Toolbox, Xml) 873 TEST(Toolbox, Xml)