diff options
-rw-r--r-- | indra/llcommon/llstring.h | 2 | ||||
-rw-r--r-- | indra/llmath/llmath.h | 21 | ||||
-rw-r--r-- | indra/llmessage/lldatapacker.cpp | 80 | ||||
-rw-r--r-- | indra/test/lldatapacker_tut.cpp | 571 | ||||
-rw-r--r-- | indra/test/lltut.h | 16 |
5 files changed, 656 insertions, 34 deletions
diff --git a/indra/llcommon/llstring.h b/indra/llcommon/llstring.h index cbfc2a2a75..c8c6e9ce7a 100644 --- a/indra/llcommon/llstring.h +++ b/indra/llcommon/llstring.h @@ -1023,7 +1023,7 @@ void LLStringBase<T>::copyInto(std::basic_string<T>& dst, const std::basic_strin } else { - LLWString tail = dst.substr(offset); + std::basic_string<T> tail = dst.substr(offset); dst = dst.substr(0, offset); dst += src; diff --git a/indra/llmath/llmath.h b/indra/llmath/llmath.h index ad8ced9e1a..5fd365086f 100644 --- a/indra/llmath/llmath.h +++ b/indra/llmath/llmath.h @@ -79,6 +79,27 @@ inline BOOL is_approx_equal(F32 x, F32 y) return (abs((S32) ((U32&)x - (U32&)y) ) < COMPARE_MANTISSA_UP_TO_BIT); } +inline BOOL is_approx_equal_fraction(F32 x, F32 y, U32 frac_bits) +{ + BOOL ret = TRUE; + F32 diff = (F32) fabs(x - y); + + S32 diffInt = (S32) diff; + S32 diffFracTolerance = (S32) ((diff - (F32) diffInt) * (1 << frac_bits)); + + // if integer portion is not equal, not enough bits were used for packing + // so error out since either the use case is not correct OR there is + // an issue with pack/unpack. should fail in either case. + // for decimal portion, make sure that the delta is no more than 1 + // based on the number of bits used for packing decimal portion. + if (diffInt != 0 || diffFracTolerance > 1) + { + ret = FALSE; + } + + return ret; +} + inline S32 llabs(const S32 a) { return S32(labs(a)); diff --git a/indra/llmessage/lldatapacker.cpp b/indra/llmessage/lldatapacker.cpp index 9670c641c2..d36e639f45 100644 --- a/indra/llmessage/lldatapacker.cpp +++ b/indra/llmessage/lldatapacker.cpp @@ -952,11 +952,11 @@ BOOL LLDataPackerAsciiBuffer::packF32(const F32 value, const char *name) int numCopied = 0; if (mWriteEnabled) { - numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%g\n", value); /* Flawfinder: ignore */ + numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%f\n", value); /* Flawfinder: ignore */ } else { - numCopied = snprintf(DUMMY_BUFFER, sizeof(DUMMY_BUFFER), "%g\n", value); /* Flawfinder: ignore */ + numCopied = snprintf(DUMMY_BUFFER, sizeof(DUMMY_BUFFER), "%f\n", value); /* Flawfinder: ignore */ } // snprintf returns number of bytes that would have been written // had the output not being truncated. In that case, it will @@ -984,7 +984,7 @@ BOOL LLDataPackerAsciiBuffer::unpackF32(F32 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g", &value); + sscanf(valuestr,"%f", &value); return success; } @@ -996,11 +996,11 @@ BOOL LLDataPackerAsciiBuffer::packColor4(const LLColor4 &value, const char *name int numCopied = 0; if (mWriteEnabled) { - numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%g %g %g %g\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ + numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%f %f %f %f\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ } else { - numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%g %g %g %g\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ + numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%f %f %f %f\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ } // snprintf returns number of bytes that would have been written // had the output not being truncated. In that case, it will @@ -1028,7 +1028,7 @@ BOOL LLDataPackerAsciiBuffer::unpackColor4(LLColor4 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g %g %g", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); + sscanf(valuestr,"%f %f %f %f", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); return success; } @@ -1089,11 +1089,11 @@ BOOL LLDataPackerAsciiBuffer::packVector2(const LLVector2 &value, const char *na int numCopied = 0; if (mWriteEnabled) { - numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%g %g\n", value.mV[0], value.mV[1]); /* Flawfinder: ignore */ + numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%f %f\n", value.mV[0], value.mV[1]); /* Flawfinder: ignore */ } else { - numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%g %g\n", value.mV[0], value.mV[1]); /* Flawfinder: ignore */ + numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%f %f\n", value.mV[0], value.mV[1]); /* Flawfinder: ignore */ } // snprintf returns number of bytes that would have been written // had the output not being truncated. In that case, it will @@ -1121,7 +1121,7 @@ BOOL LLDataPackerAsciiBuffer::unpackVector2(LLVector2 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g", &value.mV[0], &value.mV[1]); + sscanf(valuestr,"%f %f", &value.mV[0], &value.mV[1]); return success; } @@ -1133,11 +1133,11 @@ BOOL LLDataPackerAsciiBuffer::packVector3(const LLVector3 &value, const char *na int numCopied = 0; if (mWriteEnabled) { - numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%g %g %g\n", value.mV[0], value.mV[1], value.mV[2]); /* Flawfinder: ignore */ + numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%f %f %f\n", value.mV[0], value.mV[1], value.mV[2]); /* Flawfinder: ignore */ } else { - numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%g %g %g\n", value.mV[0], value.mV[1], value.mV[2]); /* Flawfinder: ignore */ + numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%f %f %f\n", value.mV[0], value.mV[1], value.mV[2]); /* Flawfinder: ignore */ } // snprintf returns number of bytes that would have been written // had the output not being truncated. In that case, it will @@ -1165,7 +1165,7 @@ BOOL LLDataPackerAsciiBuffer::unpackVector3(LLVector3 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g %g", &value.mV[0], &value.mV[1], &value.mV[2]); + sscanf(valuestr,"%f %f %f", &value.mV[0], &value.mV[1], &value.mV[2]); return success; } @@ -1176,11 +1176,11 @@ BOOL LLDataPackerAsciiBuffer::packVector4(const LLVector4 &value, const char *na int numCopied = 0; if (mWriteEnabled) { - numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%g %g %g %g\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ + numCopied = snprintf(mCurBufferp,getBufferSize()-getCurrentSize(),"%f %f %f %f\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ } else { - numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%g %g %g %g\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ + numCopied = snprintf(DUMMY_BUFFER,sizeof(DUMMY_BUFFER),"%f %f %f %f\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); /* Flawfinder: ignore */ } // snprintf returns number of bytes that would have been written // had the output not being truncated. In that case, it will @@ -1208,7 +1208,7 @@ BOOL LLDataPackerAsciiBuffer::unpackVector4(LLVector4 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g %g %g", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); + sscanf(valuestr,"%f %f %f %f", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); return success; } @@ -1339,6 +1339,19 @@ BOOL LLDataPackerAsciiBuffer::getValueStr(const char *name, char *out_value, S32 return success; } +// helper function used by LLDataPackerAsciiFile +// to convert F32 into a string. This is to avoid +// << operator writing F32 value into a stream +// since it does not seem to preserve the float value +std::string convertF32ToString(F32 val) +{ + std::string str; + char buf[20]; + snprintf(buf, 20, "%f", val); + str = buf; + return str; +} + //--------------------------------------------------------------------------- // LLDataPackerAsciiFile implementation //--------------------------------------------------------------------------- @@ -1613,11 +1626,11 @@ BOOL LLDataPackerAsciiFile::packF32(const F32 value, const char *name) writeIndentedName(name); if (mFP) { - fprintf(mFP,"%g\n", value); + fprintf(mFP,"%f\n", value); } else if (mOutputStream) { - *mOutputStream <<"" << value << "\n"; + *mOutputStream <<"" << convertF32ToString(value) << "\n"; } return success; } @@ -1632,7 +1645,7 @@ BOOL LLDataPackerAsciiFile::unpackF32(F32 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g", &value); + sscanf(valuestr,"%f", &value); return success; } @@ -1643,11 +1656,11 @@ BOOL LLDataPackerAsciiFile::packColor4(const LLColor4 &value, const char *name) writeIndentedName(name); if (mFP) { - fprintf(mFP,"%g %g %g %g\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); + fprintf(mFP,"%f %f %f %f\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); } else if (mOutputStream) { - *mOutputStream << value.mV[0] << " " << value.mV[1] << " " << value.mV[2] << " " << value.mV[3] << "\n"; + *mOutputStream << convertF32ToString(value.mV[0]) << " " << convertF32ToString(value.mV[1]) << " " << convertF32ToString(value.mV[2]) << " " << convertF32ToString(value.mV[3]) << "\n"; } return success; } @@ -1662,7 +1675,7 @@ BOOL LLDataPackerAsciiFile::unpackColor4(LLColor4 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g %g %g", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); + sscanf(valuestr,"%f %f %f %f", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); return success; } @@ -1708,11 +1721,11 @@ BOOL LLDataPackerAsciiFile::packVector2(const LLVector2 &value, const char *name writeIndentedName(name); if (mFP) { - fprintf(mFP,"%g %g\n", value.mV[0], value.mV[1]); + fprintf(mFP,"%f %f\n", value.mV[0], value.mV[1]); } else if (mOutputStream) { - *mOutputStream << value.mV[0] << " " << value.mV[1] << "\n"; + *mOutputStream << convertF32ToString(value.mV[0]) << " " << convertF32ToString(value.mV[1]) << "\n"; } return success; } @@ -1727,7 +1740,7 @@ BOOL LLDataPackerAsciiFile::unpackVector2(LLVector2 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g", &value.mV[0], &value.mV[1]); + sscanf(valuestr,"%f %f", &value.mV[0], &value.mV[1]); return success; } @@ -1738,11 +1751,11 @@ BOOL LLDataPackerAsciiFile::packVector3(const LLVector3 &value, const char *name writeIndentedName(name); if (mFP) { - fprintf(mFP,"%g %g %g\n", value.mV[0], value.mV[1], value.mV[2]); + fprintf(mFP,"%f %f %f\n", value.mV[0], value.mV[1], value.mV[2]); } else if (mOutputStream) { - *mOutputStream << value.mV[0] << " " << value.mV[1] << " " << value.mV[2] << "\n"; + *mOutputStream << convertF32ToString(value.mV[0]) << " " << convertF32ToString(value.mV[1]) << " " << convertF32ToString(value.mV[2]) << "\n"; } return success; } @@ -1757,7 +1770,7 @@ BOOL LLDataPackerAsciiFile::unpackVector3(LLVector3 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g %g", &value.mV[0], &value.mV[1], &value.mV[2]); + sscanf(valuestr,"%f %f %f", &value.mV[0], &value.mV[1], &value.mV[2]); return success; } @@ -1767,11 +1780,11 @@ BOOL LLDataPackerAsciiFile::packVector4(const LLVector4 &value, const char *name writeIndentedName(name); if (mFP) { - fprintf(mFP,"%g %g %g %g\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); + fprintf(mFP,"%f %f %f %f\n", value.mV[0], value.mV[1], value.mV[2], value.mV[3]); } else if (mOutputStream) { - *mOutputStream << value.mV[0] << " " << value.mV[1] << " " << value.mV[2] << " " << value.mV[3] << "\n"; + *mOutputStream << convertF32ToString(value.mV[0]) << " " << convertF32ToString(value.mV[1]) << " " << convertF32ToString(value.mV[2]) << " " << convertF32ToString(value.mV[3]) << "\n"; } return success; } @@ -1786,7 +1799,7 @@ BOOL LLDataPackerAsciiFile::unpackVector4(LLVector4 &value, const char *name) return FALSE; } - sscanf(valuestr,"%g %g %g %g", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); + sscanf(valuestr,"%f %f %f %f", &value.mV[0], &value.mV[1], &value.mV[2], &value.mV[3]); return success; } @@ -1828,7 +1841,8 @@ BOOL LLDataPackerAsciiFile::unpackUUID(LLUUID &value, const char *name) void LLDataPackerAsciiFile::writeIndentedName(const char *name) { - char indent_buf[64]; /*Flawfinder: ignore*/ + std::string indent_buf; + indent_buf.reserve(mIndent+1); S32 i; for(i = 0; i < mIndent; i++) @@ -1838,11 +1852,11 @@ void LLDataPackerAsciiFile::writeIndentedName(const char *name) indent_buf[i] = 0; if (mFP) { - fprintf(mFP,"%s%s\t",indent_buf, name); + fprintf(mFP,"%s%s\t",indent_buf.c_str(), name); } else if (mOutputStream) { - *mOutputStream << indent_buf << name << "\t"; + *mOutputStream << indent_buf.c_str() << name << "\t"; } } diff --git a/indra/test/lldatapacker_tut.cpp b/indra/test/lldatapacker_tut.cpp new file mode 100644 index 0000000000..71def354f9 --- /dev/null +++ b/indra/test/lldatapacker_tut.cpp @@ -0,0 +1,571 @@ +/** + * @file lldatapacker_tut.cpp + * @author Adroit + * @date February 2007 + * @brief LLDataPacker test cases. + * + * Copyright (c) 2007-2007, Linden Research, Inc. + * + * The source code in this file ("Source Code") is provided by Linden Lab + * to you under the terms of the GNU General Public License, version 2.0 + * ("GPL"), unless you have obtained a separate licensing agreement + * ("Other License"), formally executed by you and Linden Lab. Terms of + * the GPL can be found in doc/GPL-license.txt in this distribution, or + * online at http://secondlife.com/developers/opensource/gplv2 + * + * There are special exceptions to the terms and conditions of the GPL as + * it is applied to this Source Code. View the full text of the exception + * in the file doc/FLOSS-exception.txt in this software distribution, or + * online at http://secondlife.com/developers/opensource/flossexception + * + * By copying, modifying or distributing this software, you acknowledge + * that you have read and understood your obligations described above, + * and agree to abide by those obligations. + * + * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO + * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, + * COMPLETENESS OR PERFORMANCE. + */ + +#include <tut/tut.h> +#include "lltut.h" +#include "linden_common.h" +#include "lldatapacker.h" +#include "v4color.h" +#include "v4coloru.h" +#include "v2math.h" +#include "v3math.h" +#include "v4math.h" +#include "llsdserialize.h" + +#define TEST_FILE_NAME "datapacker_test.txt" +namespace tut +{ + struct datapacker_test + { + }; + typedef test_group<datapacker_test> datapacker_test_t; + typedef datapacker_test_t::object datapacker_test_object_t; + tut::datapacker_test_t tut_datapacker_test("datapacker_test"); + + //*********LLDataPackerBinaryBuffer + template<> template<> + void datapacker_test_object_t::test<1>() + { + U8 packbuf[128]; + F32 f_val1 = 44.44f, f_unpkval1; + F32 f_val2 = 12344.443232f, f_unpkval2; + F32 f_val3 = 44.4456789f, f_unpkval3; + LLDataPackerBinaryBuffer lldp(packbuf,128); + lldp.packFixed( f_val1, "linden_lab", FALSE, 8, 8); + lldp.packFixed( f_val2, "linden_lab", FALSE, 14, 16); + lldp.packFixed( f_val3, "linden_lab", FALSE, 8, 23); + + LLDataPackerBinaryBuffer lldp1(packbuf, lldp.getCurrentSize()); + lldp1.unpackFixed(f_unpkval1, "linden_lab", FALSE, 8, 8); + lldp1.unpackFixed(f_unpkval2, "linden_lab", FALSE, 14, 16); + lldp1.unpackFixed(f_unpkval3, "linden_lab", FALSE, 8, 23); + ensure_approximately_equals("LLDataPackerBinaryBuffer::packFixed 8 failed", f_val1, f_unpkval1, 8); + ensure_approximately_equals("LLDataPackerBinaryBuffer::packFixed 16 failed", f_val2, f_unpkval2, 16); + ensure_approximately_equals("LLDataPackerBinaryBuffer::packFixed 23 failed", f_val3, f_unpkval3, 31); + } + + template<> template<> + void datapacker_test_object_t::test<2>() + { + U8 packbuf[1024]; + + char str[] = "SecondLife is virtual World\0"; + char strBinary[] = "SecondLife is virtual World"; + char strBinaryFixed[] = "Fixed Data"; + S32 sizeBinaryFixed = sizeof(strBinaryFixed); + U8 valU8 = 'C'; + U16 valU16 = 0xFFFF; + U32 valU32 = 0xFFFFFFFF; + S32 valS32 = -94967295; + F32 valF32 = 4354355.44f ; + LLColor4 llcol4(3.3f, 0, 4.4f, 5.5f); + LLColor4U llcol4u(3, 128, 24, 33); + LLVector2 llvec2(333.33f, 444.44f); + LLVector3 llvec3(333.33f, 444.44f, 555.55f); + LLVector4 llvec4(333.33f, 444.44f, 555.55f, 666.66f); + LLUUID uuid; + + std::string unpkstr; + char unpkstrBinary[256]; + char unpkstrBinaryFixed[256]; + S32 unpksizeBinary; + U8 unpkvalU8; + U16 unpkvalU16; + U32 unpkvalU32; + S32 unpkvalS32; + F32 unpkvalF32; + LLColor4 unpkllcol4; + LLColor4U unpkllcol4u; + LLVector2 unpkllvec2; + LLVector3 unpkllvec3; + LLVector4 unpkllvec4; + LLUUID unpkuuid; + + LLDataPackerBinaryBuffer lldp(packbuf,1024); + lldp.packString(str , "linden_lab_str"); + lldp.packBinaryData((U8*)strBinary, sizeof(strBinary), "linden_lab_bd"); + lldp.packBinaryDataFixed((U8*)strBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp.packU8(valU8,"linden_lab_u8"); + lldp.packU16(valU16,"linden_lab_u16"); + lldp.packU32(valU32, "linden_lab_u32"); + lldp.packS32(valS32, "linden_lab_s32"); + lldp.packF32(valF32, "linden_lab_f32"); + lldp.packColor4(llcol4, "linden_lab_col4"); + lldp.packColor4U(llcol4u, "linden_lab_col4u"); + lldp.packVector2(llvec2, "linden_lab_vec2"); + lldp.packVector3(llvec3, "linden_lab_vec3"); + lldp.packVector4(llvec4, "linden_lab_vec4"); + uuid.generate(); + lldp.packUUID(uuid, "linden_lab_uuid"); + + S32 cur_size = lldp.getCurrentSize(); + + LLDataPackerBinaryBuffer lldp1(packbuf, cur_size); + lldp1.unpackString(unpkstr , "linden_lab_str"); + lldp1.unpackBinaryData((U8*)unpkstrBinary, unpksizeBinary, "linden_lab_bd"); + lldp1.unpackBinaryDataFixed((U8*)unpkstrBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp1.unpackU8(unpkvalU8,"linden_lab_u8"); + lldp1.unpackU16(unpkvalU16,"linden_lab_u16"); + lldp1.unpackU32(unpkvalU32, "linden_lab_u32"); + lldp1.unpackS32(unpkvalS32, "linden_lab_s32"); + lldp1.unpackF32(unpkvalF32, "linden_lab_f32"); + lldp1.unpackColor4(unpkllcol4, "linden_lab_col4"); + lldp1.unpackColor4U(unpkllcol4u, "linden_lab_col4u"); + lldp1.unpackVector2(unpkllvec2, "linden_lab_vec2"); + lldp1.unpackVector3(unpkllvec3, "linden_lab_vec3"); + lldp1.unpackVector4(unpkllvec4, "linden_lab_vec4"); + lldp1.unpackUUID(unpkuuid, "linden_lab_uuid"); + + ensure("LLDataPackerBinaryBuffer::packString failed", strcmp(str, unpkstr.c_str()) == 0); + ensure("LLDataPackerBinaryBuffer::packBinaryData failed", strcmp(strBinary, unpkstrBinary) == 0); + ensure("LLDataPackerBinaryBuffer::packBinaryDataFixed failed", strcmp(strBinaryFixed, unpkstrBinaryFixed) == 0); + ensure_equals("LLDataPackerBinaryBuffer::packU8 failed", valU8, unpkvalU8); + ensure_equals("LLDataPackerBinaryBuffer::packU16 failed", valU16, unpkvalU16); + ensure_equals("LLDataPackerBinaryBuffer::packU32 failed", valU32, unpkvalU32); + ensure_equals("LLDataPackerBinaryBuffer::packS32 failed", valS32, unpkvalS32); + ensure("LLDataPackerBinaryBuffer::packF32 failed", is_approx_equal(valF32, unpkvalF32)); + ensure_equals("LLDataPackerBinaryBuffer::packColor4 failed", llcol4, unpkllcol4); + ensure_equals("LLDataPackerBinaryBuffer::packColor4U failed", llcol4u, unpkllcol4u); + ensure_equals("LLDataPackerBinaryBuffer::packVector2 failed", llvec2, unpkllvec2); + ensure_equals("LLDataPackerBinaryBuffer::packVector3 failed", llvec3, unpkllvec3); + ensure_equals("LLDataPackerBinaryBuffer::packVector4 failed", llvec4, unpkllvec4); + ensure_equals("LLDataPackerBinaryBuffer::packUUID failed", uuid, unpkuuid); + } + + template<> template<> + void datapacker_test_object_t::test<3>() + { + U8 packbuf[128]; + char str[] = "SecondLife is virtual World"; + S32 strSize = sizeof(str); // include '\0' + LLDataPackerBinaryBuffer lldp(packbuf, 128); + lldp.packString(str , "linden_lab"); + + ensure("LLDataPackerBinaryBuffer: current size is wrong", strSize == lldp.getCurrentSize()); + ensure("LLDataPackerBinaryBuffer: buffer size is wrong", 128 == lldp.getBufferSize()); + + lldp.reset(); + ensure("LLDataPackerBinaryBuffer::reset failed",0 == lldp.getCurrentSize()); + } + + template<> template<> + void datapacker_test_object_t::test<4>() + { + U8* packbuf = new U8[128]; + char str[] = "SecondLife is virtual World"; + LLDataPackerBinaryBuffer lldp(packbuf, 128); + lldp.packString(str , "linden_lab"); + lldp.freeBuffer(); + ensure("LLDataPackerBinaryBuffer.freeBuffer failed" , 0 == lldp.getBufferSize()); + + } + + template<> template<> + void datapacker_test_object_t::test<5>() + { + U8 buf[] = "SecondLife is virtual World"; + S32 size = sizeof(buf); + LLDataPackerBinaryBuffer lldp(buf, size); + U8 new_buf[] = "Its Amazing"; + size = sizeof(new_buf); + lldp.assignBuffer(new_buf, size); + ensure("LLDataPackerBinaryBuffer::assignBuffer failed" , ((lldp.getBufferSize() == size) && (0 == lldp.getCurrentSize()))) ; + } + + template<> template<> + void datapacker_test_object_t::test<6>() + { + U8 packbuf[128]; + char str[] = "SecondLife is virtual World"; + LLDataPackerBinaryBuffer lldp(packbuf, 128); + lldp.packString(str , "linden_lab"); + U8 new_buffer[128]; + std::string unpkbuf; + LLDataPackerBinaryBuffer lldp1(new_buffer,128); + lldp1 = lldp; + lldp1.unpackString(unpkbuf, "linden_lab"); + ensure("1. LLDataPackerBinaryBuffer::operator= failed" , lldp1.getBufferSize() == lldp.getBufferSize()); + ensure_equals("2.LLDataPackerBinaryBuffer::operator= failed", str,unpkbuf); + } + + //*********LLDataPackerAsciiBuffer + + template<> template<> + void datapacker_test_object_t::test<7>() + { + char packbuf[128]; + F32 f_val = 44.44f, f_unpkval; + LLDataPackerAsciiBuffer lldp(packbuf,128); + lldp.packFixed( f_val, "linden_lab", FALSE, 8, 8); + + LLDataPackerAsciiBuffer lldp1(packbuf, lldp.getCurrentSize()); + lldp1.unpackFixed(f_unpkval, "linden_lab", FALSE, 8, 8); + ensure_approximately_equals("LLDataPackerAsciiBuffer::packFixed failed", f_val, f_unpkval, 8); + } + + template<> template<> + void datapacker_test_object_t::test<8>() + { + char packbuf[1024]; + + char str[] = "SecondLife is virtual World\0"; + char strBinary[] = "SecondLife is virtual World"; + char strBinaryFixed[] = "Fixed Data"; + S32 sizeBinaryFixed = sizeof(strBinaryFixed); + U8 valU8 = 'C'; + U16 valU16 = 0xFFFF; + U32 valU32 = 0xFFFFFFFF; + S32 valS32 = -94967295; + F32 valF32 = 4354355.44f ; + LLColor4 llcol4(3.3f, 0, 4.4f, 5.5f); + LLColor4U llcol4u(3, 128, 24, 33); + LLVector2 llvec2(333.33f, 444.44f); + LLVector3 llvec3(333.33f, 444.44f, 555.55f); + LLVector4 llvec4(4354355.44f, 444.44f, 555.55f, 666.66f); + LLUUID uuid; + + std::string unpkstr; + char unpkstrBinary[256]; + char unpkstrBinaryFixed[256]; + S32 unpksizeBinary; + U8 unpkvalU8; + U16 unpkvalU16; + U32 unpkvalU32; + S32 unpkvalS32; + F32 unpkvalF32; + LLColor4 unpkllcol4; + LLColor4U unpkllcol4u; + LLVector2 unpkllvec2; + LLVector3 unpkllvec3; + LLVector4 unpkllvec4; + LLUUID unpkuuid; + + LLDataPackerAsciiBuffer lldp(packbuf,1024); + lldp.packString(str , "linden_lab_str"); + lldp.packBinaryData((U8*)strBinary, sizeof(strBinary), "linden_lab_bd"); + lldp.packBinaryDataFixed((U8*)strBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp.packU8(valU8,"linden_lab_u8"); + lldp.packU16(valU16,"linden_lab_u16"); + lldp.packU32(valU32, "linden_lab_u32"); + lldp.packS32(valS32, "linden_lab_s32"); + lldp.packF32(valF32, "linden_lab_f32"); + lldp.packColor4(llcol4, "linden_lab_col4"); + lldp.packColor4U(llcol4u, "linden_lab_col4u"); + lldp.packVector2(llvec2, "linden_lab_vec2"); + lldp.packVector3(llvec3, "linden_lab_vec3"); + lldp.packVector4(llvec4, "linden_lab_vec4"); + uuid.generate(); + lldp.packUUID(uuid, "linden_lab_uuid"); + + S32 cur_size = lldp.getCurrentSize(); + + LLDataPackerAsciiBuffer lldp1(packbuf, cur_size); + lldp1.unpackString(unpkstr , "linden_lab_str"); + lldp1.unpackBinaryData((U8*)unpkstrBinary, unpksizeBinary, "linden_lab_bd"); + lldp1.unpackBinaryDataFixed((U8*)unpkstrBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp1.unpackU8(unpkvalU8,"linden_lab_u8"); + lldp1.unpackU16(unpkvalU16,"linden_lab_u16"); + lldp1.unpackU32(unpkvalU32, "linden_lab_u32"); + lldp1.unpackS32(unpkvalS32, "linden_lab_s32"); + lldp1.unpackF32(unpkvalF32, "linden_lab_f32"); + lldp1.unpackColor4(unpkllcol4, "linden_lab_col4"); + lldp1.unpackColor4U(unpkllcol4u, "linden_lab_col4u"); + lldp1.unpackVector2(unpkllvec2, "linden_lab_vec2"); + lldp1.unpackVector3(unpkllvec3, "linden_lab_vec3"); + lldp1.unpackVector4(unpkllvec4, "linden_lab_vec4"); + lldp1.unpackUUID(unpkuuid, "linden_lab_uuid"); + + ensure("LLDataPackerAsciiBuffer::packString failed", strcmp(str, unpkstr.c_str()) == 0); + ensure("LLDataPackerAsciiBuffer::packBinaryData failed", strcmp(strBinary, unpkstrBinary) == 0); + ensure("LLDataPackerAsciiBuffer::packBinaryDataFixed failed", strcmp(strBinaryFixed, unpkstrBinaryFixed) == 0); + ensure_equals("LLDataPackerAsciiBuffer::packU8 failed", valU8, unpkvalU8); + ensure_equals("LLDataPackerAsciiBuffer::packU16 failed", valU16, unpkvalU16); + ensure_equals("LLDataPackerAsciiBuffer::packU32 failed", valU32, unpkvalU32); + ensure_equals("LLDataPackerAsciiBuffer::packS32 failed", valS32, unpkvalS32); + ensure("LLDataPackerAsciiBuffer::packF32 failed", is_approx_equal(valF32, unpkvalF32)); + ensure_equals("LLDataPackerAsciiBuffer::packColor4 failed", llcol4, unpkllcol4); + ensure_equals("LLDataPackerAsciiBuffer::packColor4U failed", llcol4u, unpkllcol4u); + ensure_equals("LLDataPackerAsciiBuffer::packVector2 failed", llvec2, unpkllvec2); + ensure_equals("LLDataPackerAsciiBuffer::packVector3 failed", llvec3, unpkllvec3); + ensure_equals("LLDataPackerAsciiBuffer::packVector4 failed", llvec4, unpkllvec4); + ensure_equals("LLDataPackerAsciiBuffer::packUUID failed", uuid, unpkuuid); + } + + template<> template<> + void datapacker_test_object_t::test<9>() + { + char* packbuf = new char[128]; + char str[] = "SecondLife is virtual World"; + LLDataPackerAsciiBuffer lldp(packbuf, 128); + lldp.packString(str , "linden_lab"); + lldp.freeBuffer(); + ensure("LLDataPackerAsciiBuffer::freeBuffer failed" , 0 == lldp.getBufferSize()); + } + + template<> template<> + void datapacker_test_object_t::test<10>() + { + char buf[] = "SecondLife is virtual World"; + S32 size = sizeof(buf); + LLDataPackerAsciiBuffer lldp(buf, size); + char new_buf[] = "Its Amazing"; + size = sizeof(new_buf); + lldp.assignBuffer(new_buf, size); + ensure("LLDataPackerAsciiBuffer::assignBuffer failed" , ((lldp.getBufferSize() == size) && (1 == lldp.getCurrentSize()))) ; + } + + //*********LLDataPackerAsciiFile + + template<> template<> + void datapacker_test_object_t::test<11>() + { + F32 f_val = 44.44f, f_unpkval; + + FILE* fp = fopen(TEST_FILE_NAME, "w+"); + if(!fp) + { + llerrs << "File couldnt be open" <<llendl; + return; + } + + LLDataPackerAsciiFile lldp(fp,2); + lldp.packFixed( f_val, "linden_lab", FALSE, 8, 8); + + fflush(fp); + fseek(fp,0,SEEK_SET); + LLDataPackerAsciiFile lldp1(fp,2); + + lldp1.unpackFixed(f_unpkval, "linden_lab", FALSE, 8, 8); + fclose(fp); + + ensure_approximately_equals("LLDataPackerAsciiFile::packFixed failed", f_val, f_unpkval, 8); + } + + template<> template<> + void datapacker_test_object_t::test<12>() + { + char str[] = "SecondLife is virtual World\0"; + char strBinary[] = "SecondLife is virtual World"; + char strBinaryFixed[] = "Fixed Data"; + S32 sizeBinaryFixed = sizeof(strBinaryFixed); + U8 valU8 = 'C'; + U16 valU16 = 0xFFFF; + U32 valU32 = 0xFFFFFFFF; + S32 valS32 = -94967295; + F32 valF32 = 4354355.44f ; + LLColor4 llcol4(3.3f, 0, 4.4f, 5.5f); + LLColor4U llcol4u(3, 128, 24, 33); + LLVector2 llvec2(333.33f, 444.44f); + LLVector3 llvec3(333.33f, 444.44f, 555.55f); + LLVector4 llvec4(333.33f, 444.44f, 555.55f, 666.66f); + LLUUID uuid; + + std::string unpkstr; + char unpkstrBinary[256]; + char unpkstrBinaryFixed[256]; + S32 unpksizeBinary; + U8 unpkvalU8; + U16 unpkvalU16; + U32 unpkvalU32; + S32 unpkvalS32; + F32 unpkvalF32; + LLColor4 unpkllcol4; + LLColor4U unpkllcol4u; + LLVector2 unpkllvec2; + LLVector3 unpkllvec3; + LLVector4 unpkllvec4; + LLUUID unpkuuid; + + FILE* fp = fopen(TEST_FILE_NAME,"w+"); + if(!fp) + { + llerrs << "File couldnt be open" <<llendl; + return; + } + + LLDataPackerAsciiFile lldp(fp,2); + + lldp.packString(str , "linden_lab_str"); + lldp.packBinaryData((U8*)strBinary, sizeof(strBinary), "linden_lab_bd"); + lldp.packBinaryDataFixed((U8*)strBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp.packU8(valU8,"linden_lab_u8"); + lldp.packU16(valU16,"linden_lab_u16"); + lldp.packU32(valU32, "linden_lab_u32"); + lldp.packS32(valS32, "linden_lab_s32"); + lldp.packF32(valF32, "linden_lab_f32"); + lldp.packColor4(llcol4, "linden_lab_col4"); + lldp.packColor4U(llcol4u, "linden_lab_col4u"); + lldp.packVector2(llvec2, "linden_lab_vec2"); + lldp.packVector3(llvec3, "linden_lab_vec3"); + lldp.packVector4(llvec4, "linden_lab_vec4"); + uuid.generate(); + lldp.packUUID(uuid, "linden_lab_uuid"); + + fflush(fp); + fseek(fp,0,SEEK_SET); + LLDataPackerAsciiFile lldp1(fp,2); + + lldp1.unpackString(unpkstr , "linden_lab_str"); + lldp1.unpackBinaryData((U8*)unpkstrBinary, unpksizeBinary, "linden_lab_bd"); + lldp1.unpackBinaryDataFixed((U8*)unpkstrBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp1.unpackU8(unpkvalU8,"linden_lab_u8"); + lldp1.unpackU16(unpkvalU16,"linden_lab_u16"); + lldp1.unpackU32(unpkvalU32, "linden_lab_u32"); + lldp1.unpackS32(unpkvalS32, "linden_lab_s32"); + lldp1.unpackF32(unpkvalF32, "linden_lab_f32"); + lldp1.unpackColor4(unpkllcol4, "linden_lab_col4"); + lldp1.unpackColor4U(unpkllcol4u, "linden_lab_col4u"); + lldp1.unpackVector2(unpkllvec2, "linden_lab_vec2"); + lldp1.unpackVector3(unpkllvec3, "linden_lab_vec3"); + lldp1.unpackVector4(unpkllvec4, "linden_lab_vec4"); + lldp1.unpackUUID(unpkuuid, "linden_lab_uuid"); + + fclose(fp); + + ensure("LLDataPackerAsciiFile::packString failed", strcmp(str, unpkstr.c_str()) == 0); + ensure("LLDataPackerAsciiFile::packBinaryData failed", strcmp(strBinary, unpkstrBinary) == 0); + ensure("LLDataPackerAsciiFile::packBinaryDataFixed failed", strcmp(strBinaryFixed, unpkstrBinaryFixed) == 0); + ensure_equals("LLDataPackerAsciiFile::packU8 failed", valU8, unpkvalU8); + ensure_equals("LLDataPackerAsciiFile::packU16 failed", valU16, unpkvalU16); + ensure_equals("LLDataPackerAsciiFile::packU32 failed", valU32, unpkvalU32); + ensure_equals("LLDataPackerAsciiFile::packS32 failed", valS32, unpkvalS32); + ensure("LLDataPackerAsciiFile::packF32 failed", is_approx_equal(valF32, unpkvalF32)); + ensure_equals("LLDataPackerAsciiFile::packColor4 failed", llcol4, unpkllcol4); + ensure_equals("LLDataPackerAsciiFile::packColor4U failed", llcol4u, unpkllcol4u); + ensure_equals("LLDataPackerAsciiFile::packVector2 failed", llvec2, unpkllvec2); + ensure_equals("LLDataPackerAsciiFile::packVector3 failed", llvec3, unpkllvec3); + ensure_equals("LLDataPackerAsciiFile::packVector4 failed", llvec4, unpkllvec4); + ensure_equals("LLDataPackerAsciiFile::packUUID failed", uuid, unpkuuid); + } + + template<> template<> + void datapacker_test_object_t::test<13>() + { + F32 f_val = 44.44f, f_unpkval; + + std::ostringstream ostr; + LLDataPackerAsciiFile lldp(ostr,2); + lldp.packFixed( f_val, "linden_lab", FALSE, 8, 8); + + std::istringstream istr(ostr.str()); + LLDataPackerAsciiFile lldp1(istr,2); + + lldp1.unpackFixed(f_unpkval, "linden_lab", FALSE, 8, 8); + + ensure_approximately_equals("LLDataPackerAsciiFile::packFixed (iostring) failed", f_val, f_unpkval, 8); + } + + template<> template<> + void datapacker_test_object_t::test<14>() + { + char str[] = "SecondLife is virtual World\0"; + char strBinary[] = "SecondLife is virtual World"; + char strBinaryFixed[] = "Fixed Data"; + S32 sizeBinaryFixed = sizeof(strBinaryFixed); + U8 valU8 = 'C'; + U16 valU16 = 0xFFFF; + U32 valU32 = 0xFFFFFFFF; + S32 valS32 = -94967295; + F32 valF32 = 4354355.44f ; + LLColor4 llcol4(3.3f, 0, 4.4f, 5.5f); + LLColor4U llcol4u(3, 128, 24, 33); + LLVector2 llvec2(3333333.33f, 444.333344f); + LLVector3 llvec3(3323233.33f, 444.4324f, 555.553232f); + LLVector4 llvec4(333.33233f, 444.4323234f, 55323225.55f, 6323236.66f); + LLUUID uuid; + + std::string unpkstr; + char unpkstrBinary[256]; + char unpkstrBinaryFixed[256]; + S32 unpksizeBinary; + U8 unpkvalU8; + U16 unpkvalU16; + U32 unpkvalU32; + S32 unpkvalS32; + F32 unpkvalF32; + LLColor4 unpkllcol4; + LLColor4U unpkllcol4u; + LLVector2 unpkllvec2; + LLVector3 unpkllvec3; + LLVector4 unpkllvec4; + LLUUID unpkuuid; + + std::ostringstream ostr; + LLDataPackerAsciiFile lldp(ostr,2); + + lldp.packString(str , "linden_lab_str"); + lldp.packBinaryData((U8*)strBinary, sizeof(strBinary), "linden_lab_bd"); + lldp.packBinaryDataFixed((U8*)strBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp.packU8(valU8,"linden_lab_u8"); + lldp.packU16(valU16,"linden_lab_u16"); + lldp.packU32(valU32, "linden_lab_u32"); + lldp.packS32(valS32, "linden_lab_s32"); + lldp.packF32(valF32, "linden_lab_f32"); + lldp.packColor4(llcol4, "linden_lab_col4"); + lldp.packColor4U(llcol4u, "linden_lab_col4u"); + lldp.packVector2(llvec2, "linden_lab_vec2"); + lldp.packVector3(llvec3, "linden_lab_vec3"); + lldp.packVector4(llvec4, "linden_lab_vec4"); + uuid.generate(); + lldp.packUUID(uuid, "linden_lab_uuid"); + + std::istringstream istr(ostr.str()); + LLDataPackerAsciiFile lldp1(istr,2); + + lldp1.unpackString(unpkstr , "linden_lab_str"); + lldp1.unpackBinaryData((U8*)unpkstrBinary, unpksizeBinary, "linden_lab_bd"); + lldp1.unpackBinaryDataFixed((U8*)unpkstrBinaryFixed, sizeBinaryFixed, "linden_lab_bdf"); + lldp1.unpackU8(unpkvalU8,"linden_lab_u8"); + lldp1.unpackU16(unpkvalU16,"linden_lab_u16"); + lldp1.unpackU32(unpkvalU32, "linden_lab_u32"); + lldp1.unpackS32(unpkvalS32, "linden_lab_s32"); + lldp1.unpackF32(unpkvalF32, "linden_lab_f32"); + lldp1.unpackColor4(unpkllcol4, "linden_lab_col4"); + lldp1.unpackColor4U(unpkllcol4u, "linden_lab_col4u"); + lldp1.unpackVector2(unpkllvec2, "linden_lab_vec2"); + lldp1.unpackVector3(unpkllvec3, "linden_lab_vec3"); + lldp1.unpackVector4(unpkllvec4, "linden_lab_vec4"); + lldp1.unpackUUID(unpkuuid, "linden_lab_uuid"); + + ensure("LLDataPackerAsciiFile::packString (iostring) failed", strcmp(str, unpkstr.c_str()) == 0); + ensure("LLDataPackerAsciiFile::packBinaryData (iostring) failed", strcmp(strBinary, unpkstrBinary) == 0); + ensure("LLDataPackerAsciiFile::packBinaryDataFixed (iostring) failed", strcmp(strBinaryFixed, unpkstrBinaryFixed) == 0); + ensure_equals("LLDataPackerAsciiFile::packU8 (iostring) failed", valU8, unpkvalU8); + ensure_equals("LLDataPackerAsciiFile::packU16 (iostring) failed", valU16, unpkvalU16); + ensure_equals("LLDataPackerAsciiFile::packU32 (iostring) failed", valU32, unpkvalU32); + ensure_equals("LLDataPackerAsciiFile::packS32 (iostring) failed", valS32, unpkvalS32); + ensure("LLDataPackerAsciiFile::packF32 (iostring) failed", is_approx_equal(valF32, unpkvalF32)); + ensure_equals("LLDataPackerAsciiFile::packColor4 (iostring) failed", llcol4, unpkllcol4); + ensure_equals("LLDataPackerAsciiFile::packColor4U (iostring) failed", llcol4u, unpkllcol4u); + ensure_equals("LLDataPackerAsciiFile::packVector2 (iostring) failed", llvec2, unpkllvec2); + ensure_equals("LLDataPackerAsciiFile::packVector3 (iostring) failed", llvec3, unpkllvec3); + ensure_equals("LLDataPackerAsciiFile::packVector4 (iostring) failed", llvec4, unpkllvec4); + ensure_equals("LLDataPackerAsciiFile::packUUID (iostring) failed", uuid, unpkuuid); + } +} diff --git a/indra/test/lltut.h b/indra/test/lltut.h index 99dd71b78c..6f1fee2b2f 100644 --- a/indra/test/lltut.h +++ b/indra/test/lltut.h @@ -21,11 +21,27 @@ #include "lldate.h" #include "lluri.h" +#include "llmath.h" class LLSD; namespace tut { + inline void ensure_approximately_equals(const char* msg, F32 actual, F32 expected, U32 frac_bits) + { + if(!is_approx_equal_fraction(actual, expected, frac_bits)) + { + std::stringstream ss; + ss << (msg?msg:"") << (msg?": ":"") << "not equal actual: " << actual << " expected: " << expected; + throw tut::failure(ss.str().c_str()); + } + } + + inline void ensure_approximately_equals(F32 actual, F32 expected, U32 frac_bits) + { + ensure_approximately_equals(NULL, actual, expected, frac_bits); + } + inline void ensure_memory_matches(const char* msg,const void* actual, U32 actual_len, const void* expected,U32 expected_len) { if((expected_len != actual_len) || |