Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
9.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Unit test program for DMX testing
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "unitlib/unitlib.h"
  8. #include "dmxloader/dmxloader.h"
  9. #include "dmxloader/dmxelement.h"
  10. struct TestStruct_t
  11. {
  12. DmObjectId_t m_nId;
  13. bool m_bBool;
  14. int m_nInt;
  15. float m_flFloat;
  16. Color m_Color;
  17. Vector2D m_Vector2D;
  18. Vector m_Vector3D;
  19. Vector4D m_Vector4D;
  20. QAngle m_Angles;
  21. Quaternion m_Quaternion;
  22. VMatrix m_Matrix;
  23. char m_pStringBuf[256];
  24. };
  25. BEGIN_DMXELEMENT_UNPACK( TestStruct_t )
  26. DMXELEMENT_UNPACK_FIELD( "id_test", NULL, DmObjectId_t, m_nId )
  27. DMXELEMENT_UNPACK_FIELD( "bool_test", "1", bool, m_bBool )
  28. DMXELEMENT_UNPACK_FIELD( "int_test", "5", int, m_nInt )
  29. DMXELEMENT_UNPACK_FIELD( "float_test", "4.0", float, m_flFloat )
  30. DMXELEMENT_UNPACK_FIELD( "color_test", "200 200 200 200", Color, m_Color )
  31. DMXELEMENT_UNPACK_FIELD( "vector2d_test", "5.0 1.0", Vector2D, m_Vector2D )
  32. DMXELEMENT_UNPACK_FIELD( "vector3d_test", "5.0 1.0 -3.0", Vector, m_Vector3D )
  33. DMXELEMENT_UNPACK_FIELD( "vector4d_test", "5.0 1.0 -4.0 2.0", Vector4D, m_Vector4D )
  34. DMXELEMENT_UNPACK_FIELD( "qangle_test", "5.0 1.0 -3.0", QAngle, m_Angles )
  35. DMXELEMENT_UNPACK_FIELD( "quat_test", "5.0 1.0 -4.0 2.0", Quaternion, m_Quaternion )
  36. DMXELEMENT_UNPACK_FIELD( "vmatrix_test", NULL, VMatrix, m_Matrix )
  37. DMXELEMENT_UNPACK_FIELD_STRING( "string_test", "default", m_pStringBuf )
  38. END_DMXELEMENT_UNPACK( TestStruct_t, s_TestStructUnpack )
  39. void TestReadFile( CDmxElement *pRoot )
  40. {
  41. VMatrix mattest, mat2test;
  42. MatrixBuildRotateZ( mattest, 45 );
  43. MatrixBuildRotateZ( mat2test, 30 );
  44. int i;
  45. unsigned char buftest[256];
  46. unsigned char buf2test[256];
  47. for ( i = 0; i < 256; ++i )
  48. {
  49. buftest[i] = i;
  50. buf2test[i] = 255 - i;
  51. }
  52. // Make sure everything was read in ok.
  53. AssertEquals( pRoot->GetValue<bool>( "bool_test" ), true );
  54. AssertEquals( pRoot->GetValue<int>( "int_test" ), 2 );
  55. AssertFloatEquals( pRoot->GetValue<float>( "float_test" ), 3.0f, 1e-3 );
  56. const Color& color = pRoot->GetValue<Color>( "color_test" );
  57. Shipping_Assert( color.r() == 0 && color.g() == 64 && color.b() == 128 && color.a() == 255 );
  58. const Vector2D& vec2D = pRoot->GetValue<Vector2D>( "vector2d_test" );
  59. Shipping_Assert( vec2D.x == 1.0f && vec2D.y == -1.0f );
  60. const Vector& vec3D = pRoot->GetValue<Vector>( "vector3d_test" );
  61. Shipping_Assert( vec3D.x == 1.0f && vec3D.y == -1.0f && vec3D.z == 0.0f );
  62. const Vector4D& vec4D = pRoot->GetValue<Vector4D>( "vector4d_test" );
  63. Shipping_Assert( vec4D.x == 1.0f && vec4D.y == -1.0f && vec4D.z == 0.0f && vec4D.w == 2.0f );
  64. const QAngle& ang = pRoot->GetValue<QAngle>( "qangle_test" );
  65. Shipping_Assert( ang.x == 0.0f && ang.y == 90.0f && ang.z == -90.0f );
  66. const Quaternion& quat = pRoot->GetValue<Quaternion>( "quat_test" );
  67. Shipping_Assert( quat.x == 1.0f && quat.y == -1.0f && quat.z == 0.0f && quat.w == 2.0f );
  68. const VMatrix& mat = pRoot->GetValue<VMatrix>( "vmatrix_test" );
  69. Shipping_Assert( MatricesAreEqual( mat, mattest, 1e-3 ) );
  70. Shipping_Assert( !Q_stricmp( pRoot->GetValueString( "string_test" ), "test" ) );
  71. const CUtlBinaryBlock& blob = pRoot->GetValue<CUtlBinaryBlock>( "binary_test" );
  72. Shipping_Assert( blob.Length() == 256 );
  73. Shipping_Assert( !memcmp( blob.Get(), buftest, 256 ) );
  74. CDmxElement *pElement7 = pRoot->GetValue<CDmxElement*>( "element_test" );
  75. Shipping_Assert( pElement7 != NULL );
  76. CDmxElement *pElement6 = pRoot->GetValue<CDmxElement*>( "shared_element_test" );
  77. Shipping_Assert( pElement6 != NULL );
  78. const CUtlVector< CDmxElement* >& elementList = pRoot->GetArray<CDmxElement*>( "children" );
  79. Shipping_Assert( elementList.Count() == 2 );
  80. CDmxElement *pElement2 = elementList[0];
  81. CDmxElement *pElement3 = elementList[1];
  82. Shipping_Assert( pElement2 != NULL && pElement3 != NULL );
  83. Shipping_Assert( pElement7->GetValue<CDmxElement*>( "shared_element_test" ) == pElement6 );
  84. const CUtlVector< CDmxElement* >& elementList3 = pElement6->GetArray<CDmxElement*>( "element_array_test" );
  85. CDmxElement *pElement4 = elementList3[0];
  86. CDmxElement *pElement5 = elementList3[1];
  87. const CUtlVector< bool > &boolVec = pElement2->GetArray<bool>( "bool_array_test" );
  88. Shipping_Assert( boolVec.Count() == 2 && boolVec[0] == false && boolVec[1] == true );
  89. const CUtlVector< int > &intVec = pElement2->GetArray<int>( "int_array_test" );
  90. Shipping_Assert( intVec.Count() == 3 && intVec[0] == 0 && intVec[1] == 1 && intVec[2] == 2 );
  91. const CUtlVector< float > &floatVec = pElement2->GetArray<float>( "float_array_test" );
  92. Shipping_Assert( floatVec.Count() == 3 && floatVec[0] == -1.0f && floatVec[1] == 0.0f && floatVec[2] == 1.0f );
  93. const CUtlVector< Color > &colorVec = pElement3->GetArray<Color>( "color_array_test" );
  94. Shipping_Assert( colorVec.Count() == 3 && colorVec[0].r() == 0 && colorVec[1].r() == 64 && colorVec[2].r() == 128 );
  95. const CUtlVector< Vector2D > &vec2DVec = pElement3->GetArray<Vector2D>( "vector2d_array_test" );
  96. Shipping_Assert( vec2DVec.Count() == 2 && vec2DVec[0].x == -1.0f && vec2DVec[1].x == 1.0f );
  97. const CUtlVector< Vector > &vec3DVec = pElement3->GetArray<Vector>( "vector3d_array_test" );
  98. Shipping_Assert( vec3DVec.Count() == 2 && vec3DVec[0].x == 1.0f && vec3DVec[1].x == 2.0f );
  99. const CUtlVector< Vector4D > &vec4DVec = pElement4->GetArray<Vector4D>( "vector4d_array_test" );
  100. Shipping_Assert( vec4DVec.Count() == 2 && vec4DVec[0].x == 1.0f && vec4DVec[1].x == 2.0f );
  101. const CUtlVector< QAngle > &angVec = pElement4->GetArray<QAngle>( "qangle_array_test" );
  102. Shipping_Assert( angVec.Count() == 2 && angVec[0].x == 1.0f && angVec[1].x == 2.0f );
  103. const CUtlVector< Quaternion > &quatVec = pElement4->GetArray<Quaternion>( "quat_array_test" );
  104. Shipping_Assert( quatVec.Count() == 2 && quatVec[0].x == 1.0f && quatVec[1].x == 2.0f );
  105. const CUtlVector< VMatrix > &matVec = pElement5->GetArray<VMatrix>( "vmatrix_array_test" );
  106. Shipping_Assert( matVec.Count() == 2 );
  107. Shipping_Assert( MatricesAreEqual( matVec[0], mattest, 1e-3 ) );
  108. Shipping_Assert( MatricesAreEqual( matVec[1], mat2test, 1e-3 ) );
  109. const CUtlVector< CUtlString > &stringVec = pElement5->GetArray<CUtlString>( "string_array_test" );
  110. Shipping_Assert( stringVec.Count() == 3 && !Q_stricmp( stringVec[2], "string3" ) );
  111. const CUtlVector< CUtlBinaryBlock > &binaryVec = pElement5->GetArray<CUtlBinaryBlock>( "binary_array_test" );
  112. Shipping_Assert( binaryVec.Count() == 2 && !memcmp( binaryVec[1].Get(), buf2test, 256 ) );
  113. const CUtlVector< DmObjectId_t > &idVec = pElement6->GetArray<DmObjectId_t>( "elementid_array_test" );
  114. Shipping_Assert( idVec.Count() == 3 );
  115. TestStruct_t testStruct;
  116. pRoot->UnpackIntoStructure( &testStruct, sizeof( testStruct ), s_TestStructUnpack );
  117. Shipping_Assert( testStruct.m_bBool == true );
  118. Shipping_Assert( testStruct.m_nInt == 2 );
  119. AssertFloatEquals( testStruct.m_flFloat, 3.0f, 1e-3 );
  120. Shipping_Assert( testStruct.m_Color.r() == 0 && testStruct.m_Color.g() == 64 && testStruct.m_Color.b() == 128 && testStruct.m_Color.a() == 255 );
  121. Shipping_Assert( testStruct.m_Vector2D.x == 1.0f && testStruct.m_Vector2D.y == -1.0f );
  122. Shipping_Assert( testStruct.m_Vector3D.x == 1.0f && testStruct.m_Vector3D.y == -1.0f && testStruct.m_Vector3D.z == 0.0f );
  123. Shipping_Assert( testStruct.m_Vector4D.x == 1.0f && testStruct.m_Vector4D.y == -1.0f && testStruct.m_Vector4D.z == 0.0f && testStruct.m_Vector4D.w == 2.0f );
  124. Shipping_Assert( testStruct.m_Angles.x == 0.0f && testStruct.m_Angles.y == 90.0f && testStruct.m_Angles.z == -90.0f );
  125. Shipping_Assert( testStruct.m_Quaternion.x == 1.0f && testStruct.m_Quaternion.y == -1.0f && testStruct.m_Quaternion.z == 0.0f && testStruct.m_Quaternion.w == 2.0f );
  126. Shipping_Assert( MatricesAreEqual( testStruct.m_Matrix, mattest, 1e-3 ) );
  127. Shipping_Assert( !Q_stricmp( testStruct.m_pStringBuf, "test" ) );
  128. pElement6->UnpackIntoStructure( &testStruct, sizeof( testStruct ), s_TestStructUnpack );
  129. Shipping_Assert( testStruct.m_bBool == true );
  130. Shipping_Assert( testStruct.m_nInt == 5 );
  131. AssertFloatEquals( testStruct.m_flFloat, 4.0f, 1e-3 );
  132. Shipping_Assert( testStruct.m_Color.r() == 200 && testStruct.m_Color.g() == 200 && testStruct.m_Color.b() == 200 && testStruct.m_Color.a() == 200 );
  133. Shipping_Assert( testStruct.m_Vector2D.x == 5.0f && testStruct.m_Vector2D.y == 1.0f );
  134. Shipping_Assert( testStruct.m_Vector3D.x == 5.0f && testStruct.m_Vector3D.y == 1.0f && testStruct.m_Vector3D.z == -3.0f );
  135. Shipping_Assert( testStruct.m_Vector4D.x == 5.0f && testStruct.m_Vector4D.y == 1.0f && testStruct.m_Vector4D.z == -4.0f && testStruct.m_Vector4D.w == 2.0f );
  136. Shipping_Assert( testStruct.m_Angles.x == 5.0f && testStruct.m_Angles.y == 1.0f && testStruct.m_Angles.z == -3.0f );
  137. Shipping_Assert( testStruct.m_Quaternion.x == 5.0f && testStruct.m_Quaternion.y == 1.0f && testStruct.m_Quaternion.z == -4.0f && testStruct.m_Quaternion.w == 2.0f );
  138. Shipping_Assert( !Q_stricmp( testStruct.m_pStringBuf, "default" ) );
  139. }
  140. DEFINE_TESTCASE_NOSUITE( DmxLoaderTest )
  141. {
  142. Msg( "Running dmx loader tests...\n" );
  143. CDmxElement *pRoot;
  144. bool bOk = UnserializeDMX( "dmxtestloader.dmx", NULL, false, &pRoot );
  145. Shipping_Assert( bOk );
  146. Shipping_Assert( pRoot );
  147. if ( pRoot )
  148. {
  149. TestReadFile( pRoot );
  150. CleanupDMX( pRoot );
  151. }
  152. bOk = UnserializeDMX( "dmxtestloadertext.dmx", NULL, true, &pRoot );
  153. Shipping_Assert( bOk );
  154. Shipping_Assert( pRoot );
  155. if ( pRoot )
  156. {
  157. TestReadFile( pRoot );
  158. CleanupDMX( pRoot );
  159. }
  160. // Test serialization
  161. bOk = UnserializeDMX( "dmxtestloader.dmx", NULL, false, &pRoot );
  162. Shipping_Assert( bOk );
  163. Shipping_Assert( pRoot );
  164. if ( pRoot )
  165. {
  166. bOk = SerializeDMX( "dmxtestscratch.dmx", NULL, false, pRoot );
  167. Shipping_Assert( bOk );
  168. CleanupDMX( pRoot );
  169. }
  170. CleanupDMX( pRoot );
  171. bOk = UnserializeDMX( "dmxtestscratch.dmx", NULL, false, &pRoot );
  172. Shipping_Assert( bOk );
  173. Shipping_Assert( pRoot );
  174. if ( pRoot )
  175. {
  176. TestReadFile( pRoot );
  177. CleanupDMX( pRoot );
  178. }
  179. }