📄 byteordertest.cpp
字号:
//
// ByteOrderTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/ByteOrderTest.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "ByteOrderTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/ByteOrder.h"
using Foundation::ByteOrder;
using Foundation::Int16;
using Foundation::UInt16;
using Foundation::Int32;
using Foundation::UInt32;
#if defined(POCO_HAVE_INT64)
using Foundation::Int64;
using Foundation::UInt64;
#endif
ByteOrderTest::ByteOrderTest(const std::string& name): CppUnit::TestCase(name)
{
}
ByteOrderTest::~ByteOrderTest()
{
}
void ByteOrderTest::testByteOrderFlip()
{
{
Int16 norm = (Int16) 0xAABB;
Int16 flip = ByteOrder::flipBytes(norm);
assert (UInt16(flip) == 0xBBAA);
flip = ByteOrder::flipBytes(flip);
assert (flip == norm);
}
{
UInt16 norm = (UInt16) 0xAABB;
UInt16 flip = ByteOrder::flipBytes(norm);
assert (flip == 0xBBAA);
flip = ByteOrder::flipBytes(flip);
assert (flip == norm);
}
{
Int32 norm = 0xAABBCCDD;
Int32 flip = ByteOrder::flipBytes(norm);
assert (UInt32(flip) == 0xDDCCBBAA);
flip = ByteOrder::flipBytes(flip);
assert (flip == norm);
}
{
UInt32 norm = 0xAABBCCDD;
UInt32 flip = ByteOrder::flipBytes(norm);
assert (flip == 0xDDCCBBAA);
flip = ByteOrder::flipBytes(flip);
assert (flip == norm);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = (Int64(0x8899AABB) << 32) + 0xCCDDEEFF;
Int64 flip = ByteOrder::flipBytes(norm);
assert (flip == (Int64(0xFFEEDDCC) << 32) + 0xBBAA9988);
flip = ByteOrder::flipBytes(flip);
assert (flip == norm);
}
{
UInt64 norm = (UInt64(0x8899AABB) << 32) + 0xCCDDEEFF;
UInt64 flip = ByteOrder::flipBytes(norm);
assert (flip == (UInt64(0xFFEEDDCC) << 32) + 0xBBAA9988);
flip = ByteOrder::flipBytes(flip);
assert (flip == norm);
}
#endif
}
void ByteOrderTest::testByteOrderBigEndian()
{
#if defined(POCO_ARCH_BIG_ENDIAN)
//
// big-endian systems
//
{
Int16 norm = 4;
Int16 flip = ByteOrder::toBigEndian(norm);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::toBigEndian(norm);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::toBigEndian(norm);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::toBigEndian(norm);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::toBigEndian(norm);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::toBigEndian(norm);
assert (norm == flip);
}
#endif
{
Int16 norm = 4;
Int16 flip = ByteOrder::fromBigEndian(norm);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::fromBigEndian(norm);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::fromBigEndian(norm);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::fromBigEndian(norm);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::fromBigEndian(norm);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::fromBigEndian(norm);
assert (norm == flip);
}
#endif
#else
//
// little-endian systems
//
{
Int16 norm = 4;
Int16 flip = ByteOrder::toBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::toBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::toBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::toBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::toBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::toBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#endif
{
Int16 norm = 4;
Int16 flip = ByteOrder::fromBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::fromBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::fromBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::fromBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::fromBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::fromBigEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#endif
#endif
}
void ByteOrderTest::testByteOrderLittleEndian()
{
#if defined(POCO_ARCH_LITTLE_ENDIAN)
//
// big-endian systems
//
{
Int16 norm = 4;
Int16 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
#endif
{
Int16 norm = 4;
Int16 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::toLittleEndian(norm);
assert (norm == flip);
}
#endif
#else
//
// little-endian systems
//
{
Int16 norm = 4;
Int16 flip = ByteOrder::toLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::toLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::toLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::toLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::toLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::toLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#endif
{
Int16 norm = 4;
Int16 flip = ByteOrder::fromLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::fromLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::fromLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::fromLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::fromLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::fromLittleEndian(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#endif
#endif
}
void ByteOrderTest::testByteOrderNetwork()
{
#if defined(POCO_ARCH_BIG_ENDIAN)
//
// big-endian systems
//
{
Int16 norm = 4;
Int16 flip = ByteOrder::toNetwork(norm);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::toNetwork(norm);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::toNetwork(norm);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::toNetwork(norm);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::toNetwork(norm);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::toNetwork(norm);
assert (norm == flip);
}
#endif
{
Int16 norm = 4;
Int16 flip = ByteOrder::fromNetwork(norm);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::fromNetwork(norm);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::fromNetwork(norm);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::fromNetwork(norm);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::fromNetwork(norm);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::fromNetwork(norm);
assert (norm == flip);
}
#endif
#else
//
// little-endian systems
//
{
Int16 norm = 4;
Int16 flip = ByteOrder::toNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::toNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::toNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::toNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::toNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::toNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#endif
{
Int16 norm = 4;
Int16 flip = ByteOrder::fromNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt16 norm = 4;
UInt16 flip = ByteOrder::fromNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
Int32 norm = 4;
Int32 flip = ByteOrder::fromNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt32 norm = 4;
UInt32 flip = ByteOrder::fromNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#if defined(POCO_HAVE_INT64)
{
Int64 norm = 4;
Int64 flip = ByteOrder::fromNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
{
UInt64 norm = 4;
UInt64 flip = ByteOrder::fromNetwork(norm);
assert (norm != flip);
flip = ByteOrder::flipBytes(flip);
assert (norm == flip);
}
#endif
#endif
}
void ByteOrderTest::setUp()
{
}
void ByteOrderTest::tearDown()
{
}
CppUnit::Test* ByteOrderTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ByteOrderTest");
CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderFlip);
CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderBigEndian);
CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderLittleEndian);
CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderNetwork);
return pSuite;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -