Add ByteVector conversion functions, tests

This commit is contained in:
complexlogic 2023-10-14 10:48:47 -07:00 committed by Urs Fleisch
parent 6be03b7ae1
commit b40b834b1b
3 changed files with 91 additions and 7 deletions

View File

@ -292,11 +292,21 @@ ByteVector ByteVector::fromShort(short value, bool mostSignificantByteFirst)
return fromNumber<unsigned short>(value, mostSignificantByteFirst);
}
ByteVector ByteVector::fromUShort(unsigned short value, bool mostSignificantByteFirst)
{
return fromNumber<unsigned short>(value, mostSignificantByteFirst);
}
ByteVector ByteVector::fromLongLong(long long value, bool mostSignificantByteFirst)
{
return fromNumber<unsigned long long>(value, mostSignificantByteFirst);
}
ByteVector ByteVector::fromULongLong(unsigned long long value, bool mostSignificantByteFirst)
{
return fromNumber<unsigned long long>(value, mostSignificantByteFirst);
}
ByteVector ByteVector::fromFloat32LE(float value)
{
return fromFloat<float, unsigned int, Utils::LittleEndian>(value);
@ -693,6 +703,16 @@ long long ByteVector::toLongLong(unsigned int offset, bool mostSignificantByteFi
return toNumber<unsigned long long>(*this, offset, mostSignificantByteFirst);
}
unsigned long long ByteVector::toULongLong(bool mostSignificantByteFirst) const
{
return toNumber<unsigned long long>(*this, 0, mostSignificantByteFirst);
}
unsigned long long ByteVector::toULongLong(unsigned int offset, bool mostSignificantByteFirst) const
{
return toNumber<unsigned long long>(*this, offset, mostSignificantByteFirst);
}
float ByteVector::toFloat32LE(size_t offset) const
{
return toFloat<float, unsigned int, Utils::LittleEndian>(*this, offset);

View File

@ -349,24 +349,24 @@ namespace TagLib {
short toShort(unsigned int offset, bool mostSignificantByteFirst = true) const;
/*!
* Converts the first 2 bytes of the vector to a unsigned short.
* Converts the first 2 bytes of the vector to an unsigned short.
*
* If \a mostSignificantByteFirst is true this will operate left to right
* evaluating the integer. For example if \a mostSignificantByteFirst is
* true then $00 $01 == 0x0001 == 1, if false, $01 00 == 0x01000000 == 1.
*
* \see fromShort()
* \see fromUShort()
*/
unsigned short toUShort(bool mostSignificantByteFirst = true) const;
/*!
* Converts the 2 bytes at \a offset of the vector to a unsigned short.
* Converts the 2 bytes at \a offset of the vector to an unsigned short.
*
* If \a mostSignificantByteFirst is true this will operate left to right
* evaluating the integer. For example if \a mostSignificantByteFirst is
* true then $00 $01 == 0x0001 == 1, if false, $01 00 == 0x01000000 == 1.
*
* \see fromShort()
* \see fromUShort()
*/
unsigned short toUShort(unsigned int offset, bool mostSignificantByteFirst = true) const;
@ -378,7 +378,7 @@ namespace TagLib {
* true then $00 00 00 00 00 00 00 01 == 0x0000000000000001 == 1,
* if false, $01 00 00 00 00 00 00 00 == 0x0100000000000000 == 1.
*
* \see fromUInt()
* \see fromLongLong()
*/
long long toLongLong(bool mostSignificantByteFirst = true) const;
@ -390,10 +390,34 @@ namespace TagLib {
* true then $00 00 00 00 00 00 00 01 == 0x0000000000000001 == 1,
* if false, $01 00 00 00 00 00 00 00 == 0x0100000000000000 == 1.
*
* \see fromUInt()
* \see fromLongLong()
*/
long long toLongLong(unsigned int offset, bool mostSignificantByteFirst = true) const;
/*!
* Converts the first 8 bytes of the vector to an unsigned long long.
*
* If \a mostSignificantByteFirst is true this will operate left to right
* evaluating the integer. For example if \a mostSignificantByteFirst is
* true then $00 00 00 00 00 00 00 01 == 0x0000000000000001 == 1,
* if false, $01 00 00 00 00 00 00 00 == 0x0100000000000000 == 1.
*
* \see fromULongLong()
*/
unsigned long long toULongLong(bool mostSignificantByteFirst = true) const;
/*!
* Converts the 8 bytes at \a offset of the vector to an unsigned long long.
*
* If \a mostSignificantByteFirst is true this will operate left to right
* evaluating the integer. For example if \a mostSignificantByteFirst is
* true then $00 00 00 00 00 00 00 01 == 0x0000000000000001 == 1,
* if false, $01 00 00 00 00 00 00 00 == 0x0100000000000000 == 1.
*
* \see fromULongLong()
*/
unsigned long long toULongLong(unsigned int offset, bool mostSignificantByteFirst = true) const;
/*
* Converts the 4 bytes at \a offset of the vector to a float as an IEEE754
* 32-bit little-endian floating point number.
@ -455,6 +479,16 @@ namespace TagLib {
*/
static ByteVector fromShort(short value, bool mostSignificantByteFirst = true);
/*!
* Creates a 2 byte ByteVector based on \a value. If
* \a mostSignificantByteFirst is true, then this will operate left to right
* in building the ByteVector. For example if \a mostSignificantByteFirst is
* true then $00 01 == 0x0001 == 1, if false, $01 00 == 0x0100 == 1.
*
* \see toUShort()
*/
static ByteVector fromUShort(unsigned short value, bool mostSignificantByteFirst = true);
/*!
* Creates a 8 byte ByteVector based on \a value. If
* \a mostSignificantByteFirst is true, then this will operate left to right
@ -466,6 +500,17 @@ namespace TagLib {
*/
static ByteVector fromLongLong(long long value, bool mostSignificantByteFirst = true);
/*!
* Creates a 8 byte ByteVector based on \a value. If
* \a mostSignificantByteFirst is true, then this will operate left to right
* in building the ByteVector. For example if \a mostSignificantByteFirst is
* true then $00 00 00 01 == 0x0000000000000001 == 1, if false,
* $01 00 00 00 00 00 00 00 == 0x0100000000000000 == 1.
*
* \see toULongLong()
*/
static ByteVector fromULongLong(unsigned long long value, bool mostSignificantByteFirst = true);
/*!
* Creates a 4 byte ByteVector based on \a value as an IEEE754 32-bit
* little-endian floating point number.

View File

@ -189,6 +189,8 @@ public:
CPPUNIT_ASSERT_EQUAL(static_cast<short>(0x01ff), data.toShort(5U, false));
CPPUNIT_ASSERT_EQUAL(static_cast<short>(0xff), data.toShort(13U));
CPPUNIT_ASSERT_EQUAL(static_cast<short>(0xff), data.toShort(13U, false));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromShort(0x00ff), ByteVector("\x00\xff", 2));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromShort(0x00ff, false), ByteVector("\xff\x00", 2));
CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0x00ff), data.toUShort());
CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0xff00), data.toUShort(false));
@ -196,6 +198,8 @@ public:
CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0x01ff), data.toUShort(5U, false));
CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0xff), data.toUShort(13U));
CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(0xff), data.toUShort(13U, false));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromUShort(0xff00), ByteVector("\xff\x00", 2));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromUShort(0xff00, false), ByteVector("\x00\xff", 2));
CPPUNIT_ASSERT_EQUAL(0x00ff01ffU, data.toUInt());
CPPUNIT_ASSERT_EQUAL(0xff01ff00U, data.toUInt(false));
@ -210,6 +214,8 @@ public:
CPPUNIT_ASSERT_EQUAL(0xff01ffU, data.toUInt(5U, 3U, false));
CPPUNIT_ASSERT_EQUAL(0x00ffU, data.toUInt(12U, 3U));
CPPUNIT_ASSERT_EQUAL(0xff00U, data.toUInt(12U, 3U, false));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromUInt(0xff00ff00), ByteVector("\xff\x00\xff\x00", 4));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromUInt(0xff00ff00, false), ByteVector("\x00\xff\x00\xff", 4));
CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0x00ff01ff00ff01ffULL), data.toLongLong());
CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0xff01ff00ff01ff00ULL), data.toLongLong(false));
@ -217,7 +223,20 @@ public:
CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0x00ff01ff00ff01ffULL), data.toLongLong(5U, false));
CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0x00ffU), data.toLongLong(12U));
CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0xff00U), data.toLongLong(12U, false));
}
CPPUNIT_ASSERT_EQUAL(ByteVector::fromLongLong(0x00ff01ff00ff01ff), ByteVector("\x00\xff\x01\xff\x00\xff\x01\xff", 8));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromLongLong(0x00ff01ff00ff01ff, false), ByteVector("\xff\x01\xff\x00\xff\x01\xff\x00", 8));
const ByteVector data2("\xff\x01\xff\x00\xff\x01\xff\x00\xff\x01\xff\x00\xff\x01", 14);
CPPUNIT_ASSERT_EQUAL(0xff01ff00ff01ff00ULL, data2.toULongLong());
CPPUNIT_ASSERT_EQUAL(0x00ff01ff00ff01ffULL, data2.toULongLong(false));
CPPUNIT_ASSERT_EQUAL(0x01ff00ff01ff00ffULL, data2.toULongLong(5U));
CPPUNIT_ASSERT_EQUAL(0xff00ff01ff00ff01ULL, data2.toULongLong(5U, false));
CPPUNIT_ASSERT_EQUAL(0xff01ULL, data2.toULongLong(12U));
CPPUNIT_ASSERT_EQUAL(0x01ffULL, data2.toULongLong(12U, false));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromULongLong(0xff01ff00ff01ff00ULL), ByteVector("\xff\x01\xff\x00\xff\x01\xff\x00", 8));
CPPUNIT_ASSERT_EQUAL(ByteVector::fromULongLong(0xff01ff00ff01ff00ULL, false), ByteVector("\x00\xff\x01\xff\x00\xff\x01\xff", 8));
}
void testFloatingPointConversion()
{