Mercurial > hg > orthanc
comparison UnitTestsSources/UnitTestsMain.cpp @ 1938:48ad54f7b21f
endianness
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Mar 2016 10:24:51 +0100 |
parents | ff11ba08e5d0 |
children | d80a4fe8ffcc |
comparison
equal
deleted
inserted
replaced
1937:3756bedaaa36 | 1938:48ad54f7b21f |
---|---|
685 #error Support your platform here | 685 #error Support your platform here |
686 #endif | 686 #endif |
687 } | 687 } |
688 | 688 |
689 | 689 |
690 #include "../Core/Endianness.h" | |
691 | |
692 static void ASSERT_EQ16(uint16_t a, uint16_t b) | |
693 { | |
694 // This cast solves a linking problem with MinGW | |
695 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); | |
696 } | |
697 | |
698 | |
699 static void ASSERT_NE16(uint16_t a, uint16_t b) | |
700 { | |
701 // This cast solves a linking problem with MinGW | |
702 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); | |
703 } | |
704 | |
705 | |
706 TEST(Toolbox, EndiannessConversions16) | |
707 { | |
708 Endianness e = Toolbox::DetectEndianness(); | |
709 | |
710 for (unsigned int i = 0; i < 65536; i++) | |
711 { | |
712 uint16_t v = static_cast<uint16_t>(i); | |
713 ASSERT_EQ16(v, be16toh(htobe16(v))); | |
714 ASSERT_EQ16(v, le16toh(htole16(v))); | |
715 | |
716 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&v); | |
717 if (bytes[0] != bytes[1]) | |
718 { | |
719 ASSERT_NE16(v, le16toh(htobe16(v))); | |
720 ASSERT_NE16(v, be16toh(htole16(v))); | |
721 } | |
722 else | |
723 { | |
724 ASSERT_EQ16(v, le16toh(htobe16(v))); | |
725 ASSERT_EQ16(v, be16toh(htole16(v))); | |
726 } | |
727 | |
728 switch (e) | |
729 { | |
730 case Endianness_Little: | |
731 ASSERT_EQ16(v, htole16(v)); | |
732 if (bytes[0] != bytes[1]) | |
733 { | |
734 ASSERT_NE16(v, htobe16(v)); | |
735 } | |
736 else | |
737 { | |
738 ASSERT_EQ16(v, htobe16(v)); | |
739 } | |
740 break; | |
741 | |
742 case Endianness_Big: | |
743 ASSERT_EQ16(v, htobe16(v)); | |
744 if (bytes[0] != bytes[1]) | |
745 { | |
746 ASSERT_NE16(v, htole16(v)); | |
747 } | |
748 else | |
749 { | |
750 ASSERT_EQ16(v, htole16(v)); | |
751 } | |
752 break; | |
753 | |
754 default: | |
755 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
756 } | |
757 } | |
758 } | |
759 | |
760 | |
761 | |
762 | |
763 | |
690 #if ORTHANC_PUGIXML_ENABLED == 1 | 764 #if ORTHANC_PUGIXML_ENABLED == 1 |
691 TEST(Toolbox, Xml) | 765 TEST(Toolbox, Xml) |
692 { | 766 { |
693 Json::Value a; | 767 Json::Value a; |
694 a["hello"] = "world"; | 768 a["hello"] = "world"; |