moved ShapeInfo tests to their own file

This commit is contained in:
Andrew Meadows 2014-11-03 15:25:04 -08:00
parent aa0172cf8b
commit 4a1133fbc2
6 changed files with 312 additions and 173 deletions

View file

@ -38,11 +38,9 @@ public:
static unsigned int hashFunction(unsigned int value, int primeIndex);
static unsigned int hashFunction2(unsigned int value);
private:
friend class ShapeManager;
btCollisionShape* createShape() const;
private:
int _type;
btAlignedObjectArray<btVector3> _data;
};

View file

@ -0,0 +1,266 @@
//
// ShapeInfoTests.cpp
// tests/physics/src
//
// Created by Andrew Meadows on 2014.11.02
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <iostream>
#include <ShapeInfo.h>
#include <StreamUtils.h>
#include "ShapeInfoTests.h"
void ShapeInfoTests::testHashFunctions() {
#ifdef USE_BULLET_PHYSICS
int maxTests = 10000000;
ShapeInfo info;
btHashMap<btHashInt, int> hashes;
int bits[32];
unsigned int masks[32];
for (int i = 0; i < 32; ++i) {
bits[i] = 0;
masks[i] = 1U << i;
}
float deltaLength = 0.002f;
float endLength = 100.0f;
int numSteps = (int)(endLength / deltaLength);
int testCount = 0;
int numCollisions = 0;
btClock timer;
for (int x = 1; x < numSteps && testCount < maxTests; ++x) {
float radiusX = (float)x * deltaLength;
// test sphere
info.setSphere(radiusX);
++testCount;
int hash = info.computeHash();
int hash2 = info.computeHash2();
int* hashPtr = hashes.find(hash);
if (hashPtr && *hashPtr == hash2) {
std::cout << testCount << " hash collision radiusX = " << radiusX
<< " h1 = 0x" << std::hex << (unsigned int)(hash)
<< " h2 = 0x" << std::hex << (unsigned int)(hash2)
<< std::endl;
++numCollisions;
assert(false);
} else {
hashes.insert(hash, hash2);
}
for (int k = 0; k < 32; ++k) {
if (masks[k] & hash2) {
++bits[k];
}
}
for (int y = 1; y < numSteps && testCount < maxTests; ++y) {
float radiusY = (float)y * deltaLength;
// test cylinder and capsule
int types[] = { CYLINDER_SHAPE_PROXYTYPE, CAPSULE_SHAPE_PROXYTYPE };
for (int i = 0; i < 2; ++i) {
switch(types[i]) {
case CYLINDER_SHAPE_PROXYTYPE: {
info.setCylinder(radiusX, radiusY);
break;
}
case CAPSULE_SHAPE_PROXYTYPE: {
info.setCapsule(radiusX, radiusY);
break;
}
}
++testCount;
hash = info.computeHash();
hash2 = info.computeHash2();
hashPtr = hashes.find(hash);
if (hashPtr && *hashPtr == hash2) {
std::cout << testCount << " hash collision radiusX = " << radiusX << " radiusY = " << radiusY
<< " h1 = 0x" << std::hex << (unsigned int)(hash)
<< " h2 = 0x" << std::hex << (unsigned int)(hash2)
<< std::endl;
++numCollisions;
assert(false);
} else {
hashes.insert(hash, hash2);
}
for (int k = 0; k < 32; ++k) {
if (masks[k] & hash2) {
++bits[k];
}
}
}
for (int z = 1; z < numSteps && testCount < maxTests; ++z) {
float radiusZ = (float)z * deltaLength;
// test box
info.setBox(btVector3(radiusX, radiusY, radiusZ));
++testCount;
hash = info.computeHash();
hash2 = info.computeHash2();
hashPtr = hashes.find(hash);
if (hashPtr && *hashPtr == hash2) {
std::cout << testCount << " hash collision radiusX = " << radiusX
<< " radiusY = " << radiusY << " radiusZ = " << radiusZ
<< " h1 = 0x" << std::hex << (unsigned int)(hash)
<< " h2 = 0x" << std::hex << (unsigned int)(hash2)
<< std::endl;
++numCollisions;
assert(false);
} else {
hashes.insert(hash, hash2);
}
for (int k = 0; k < 32; ++k) {
if (masks[k] & hash2) {
++bits[k];
}
}
}
}
}
unsigned long int msec = timer.getTimeMilliseconds();
std::cout << msec << " msec with " << numCollisions << " collisions out of " << testCount << " hashes" << std::endl;
// print out distribution of bits
for (int i = 0; i < 32; ++i) {
std::cout << "bit 0x" << std::hex << masks[i] << std::dec << " = " << bits[i] << std::endl;
}
#endif // USE_BULLET_PHYSICS
}
void ShapeInfoTests::testBoxShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
btVector3 halfExtents(1.23f, 4.56f, 7.89f);
info.setBox(halfExtents);
int hash = info.computeHash();
int hash2 = info.computeHash2();
btCollisionShape* shape = info.createShape();
if (!shape) {
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: NULL Box shape" << std::endl;
}
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Box shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
int otherHash2= otherInfo.computeHash2();
if (hash2 != otherHash2) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Box shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl;
}
delete shape;
#endif // USE_BULLET_PHYSICS
}
void ShapeInfoTests::testSphereShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
float radius = 1.23f;
info.setSphere(radius);
int hash = info.computeHash();
int hash2 = info.computeHash2();
btCollisionShape* shape = info.createShape();
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Sphere shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
int otherHash2 = otherInfo.computeHash2();
if (hash2 != otherHash2) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Sphere shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl;
}
delete shape;
#endif // USE_BULLET_PHYSICS
}
void ShapeInfoTests::testCylinderShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
float radius = 1.23f;
float height = 4.56f;
info.setCylinder(radius, height);
int hash = info.computeHash();
int hash2 = info.computeHash2();
btCollisionShape* shape = info.createShape();
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Cylinder shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
int otherHash2 = otherInfo.computeHash2();
if (hash2 != otherHash2) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Cylinder shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl;
}
delete shape;
#endif // USE_BULLET_PHYSICS
}
void ShapeInfoTests::testCapsuleShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
float radius = 1.23f;
float height = 4.56f;
info.setCapsule(radius, height);
int hash = info.computeHash();
int hash2 = info.computeHash2();
btCollisionShape* shape = info.createShape();
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Capsule shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
int otherHash2 = otherInfo.computeHash2();
if (hash2 != otherHash2) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Capsule shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl;
}
delete shape;
#endif // USE_BULLET_PHYSICS
}
void ShapeInfoTests::runAllTests() {
//#define MANUAL_TEST
#ifdef MANUAL_TEST
testHashFunctions();
#endif // MANUAL_TEST
testBoxShape();
testSphereShape();
testCylinderShape();
testCapsuleShape();
}

View file

@ -0,0 +1,24 @@
//
// ShapeInfoTests.h
// tests/physics/src
//
// Created by Andrew Meadows on 2014.11.02
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_ShapeInfoTests_h
#define hifi_ShapeInfoTests_h
namespace ShapeInfoTests {
void testHashFunctions();
void testBoxShape();
void testSphereShape();
void testCylinderShape();
void testCapsuleShape();
void runAllTests();
}
#endif // hifi_ShapeInfoTests_h

View file

@ -15,124 +15,6 @@
#include "ShapeManagerTests.h"
void ShapeManagerTests::testHashFunctions() {
#ifdef USE_BULLET_PHYSICS
int maxTests = 10000000;
ShapeInfo info;
btHashMap<btHashInt, int> hashes;
int bits[32];
unsigned int masks[32];
for (int i = 0; i < 32; ++i) {
bits[i] = 0;
masks[i] = 1U << i;
}
float deltaLength = 0.002f;
float endLength = 100.0f;
int numSteps = (int)(endLength / deltaLength);
int testCount = 0;
int numCollisions = 0;
btClock timer;
for (int x = 1; x < numSteps && testCount < maxTests; ++x) {
float radiusX = (float)x * deltaLength;
// test sphere
info.setSphere(radiusX);
++testCount;
int hash = info.computeHash();
int hash2 = info.computeHash2();
int* hashPtr = hashes.find(hash);
if (hashPtr && *hashPtr == hash2) {
std::cout << testCount << " hash collision radiusX = " << radiusX
<< " h1 = 0x" << std::hex << (unsigned int)(hash)
<< " h2 = 0x" << std::hex << (unsigned int)(hash2)
<< std::endl;
++numCollisions;
assert(false);
} else {
hashes.insert(hash, hash2);
}
for (int k = 0; k < 32; ++k) {
if (masks[k] & hash2) {
++bits[k];
}
}
for (int y = 1; y < numSteps && testCount < maxTests; ++y) {
float radiusY = (float)y * deltaLength;
// test cylinder and capsule
int types[] = { CYLINDER_SHAPE_PROXYTYPE, CAPSULE_SHAPE_PROXYTYPE };
for (int i = 0; i < 2; ++i) {
switch(types[i]) {
case CYLINDER_SHAPE_PROXYTYPE: {
info.setCylinder(radiusX, radiusY);
break;
}
case CAPSULE_SHAPE_PROXYTYPE: {
info.setCapsule(radiusX, radiusY);
break;
}
}
++testCount;
hash = info.computeHash();
hash2 = info.computeHash2();
hashPtr = hashes.find(hash);
if (hashPtr && *hashPtr == hash2) {
std::cout << testCount << " hash collision radiusX = " << radiusX << " radiusY = " << radiusY
<< " h1 = 0x" << std::hex << (unsigned int)(hash)
<< " h2 = 0x" << std::hex << (unsigned int)(hash2)
<< std::endl;
++numCollisions;
assert(false);
} else {
hashes.insert(hash, hash2);
}
for (int k = 0; k < 32; ++k) {
if (masks[k] & hash2) {
++bits[k];
}
}
}
for (int z = 1; z < numSteps && testCount < maxTests; ++z) {
float radiusZ = (float)z * deltaLength;
// test box
info.setBox(btVector3(radiusX, radiusY, radiusZ));
++testCount;
hash = info.computeHash();
hash2 = info.computeHash2();
hashPtr = hashes.find(hash);
if (hashPtr && *hashPtr == hash2) {
std::cout << testCount << " hash collision radiusX = " << radiusX
<< " radiusY = " << radiusY << " radiusZ = " << radiusZ
<< " h1 = 0x" << std::hex << (unsigned int)(hash)
<< " h2 = 0x" << std::hex << (unsigned int)(hash2)
<< std::endl;
++numCollisions;
assert(false);
} else {
hashes.insert(hash, hash2);
}
for (int k = 0; k < 32; ++k) {
if (masks[k] & hash2) {
++bits[k];
}
}
}
}
}
unsigned long int msec = timer.getTimeMilliseconds();
std::cout << msec << " msec with " << numCollisions << " collisions out of " << testCount << " hashes" << std::endl;
// print out distribution of bits
for (int i = 0; i < 32; ++i) {
std::cout << "bit 0x" << std::hex << masks[i] << std::dec << " = " << bits[i] << std::endl;
}
#endif // USE_BULLET_PHYSICS
}
void ShapeManagerTests::testShapeAccounting() {
#ifdef USE_BULLET_PHYSICS
ShapeManager shapeManager;
@ -271,26 +153,19 @@ void ShapeManagerTests::addManyShapes() {
#endif // USE_BULLET_PHYSICS
}
void ShapeManagerTests::testBoxShape() {
void ShapeManagerTests::addBoxShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
btVector3 halfExtents(1.23f, 4.56f, 7.89f);
info.setBox(halfExtents);
int hash = info.computeHash();
ShapeManager shapeManager;
btCollisionShape* shape = shapeManager.getShape(info);
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Box shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
btCollisionShape* otherShape = shapeManager.getShape(info);
btCollisionShape* otherShape = shapeManager.getShape(otherInfo);
if (shape != otherShape) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: Box ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
@ -298,26 +173,19 @@ void ShapeManagerTests::testBoxShape() {
#endif // USE_BULLET_PHYSICS
}
void ShapeManagerTests::testSphereShape() {
void ShapeManagerTests::addSphereShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
float radius = 1.23f;
info.setSphere(radius);
int hash = info.computeHash();
ShapeManager shapeManager;
btCollisionShape* shape = shapeManager.getShape(info);
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Sphere shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
btCollisionShape* otherShape = shapeManager.getShape(info);
btCollisionShape* otherShape = shapeManager.getShape(otherInfo);
if (shape != otherShape) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: Sphere ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
@ -325,27 +193,20 @@ void ShapeManagerTests::testSphereShape() {
#endif // USE_BULLET_PHYSICS
}
void ShapeManagerTests::testCylinderShape() {
void ShapeManagerTests::addCylinderShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
float radius = 1.23f;
float height = 4.56f;
info.setCylinder(radius, height);
int hash = info.computeHash();
ShapeManager shapeManager;
btCollisionShape* shape = shapeManager.getShape(info);
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Cylinder shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
btCollisionShape* otherShape = shapeManager.getShape(info);
btCollisionShape* otherShape = shapeManager.getShape(otherInfo);
if (shape != otherShape) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: Cylinder ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
@ -353,27 +214,20 @@ void ShapeManagerTests::testCylinderShape() {
#endif // USE_BULLET_PHYSICS
}
void ShapeManagerTests::testCapsuleShape() {
void ShapeManagerTests::addCapsuleShape() {
#ifdef USE_BULLET_PHYSICS
ShapeInfo info;
float radius = 1.23f;
float height = 4.56f;
info.setCapsule(radius, height);
int hash = info.computeHash();
ShapeManager shapeManager;
btCollisionShape* shape = shapeManager.getShape(info);
ShapeInfo otherInfo;
otherInfo.collectInfo(shape);
int otherHash = otherInfo.computeHash();
if (hash != otherHash) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: expected Capsule shape hash = " << hash << " but found hash = " << otherHash << std::endl;
}
btCollisionShape* otherShape = shapeManager.getShape(info);
btCollisionShape* otherShape = shapeManager.getShape(otherInfo);
if (shape != otherShape) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: Capsule ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
@ -382,14 +236,10 @@ void ShapeManagerTests::testCapsuleShape() {
}
void ShapeManagerTests::runAllTests() {
//#define MANUAL_TEST
#ifdef MANUAL_TEST
testHashFunctions();
#endif // MANUAL_TEST
testShapeAccounting();
addManyShapes();
testBoxShape();
testSphereShape();
testCylinderShape();
testCapsuleShape();
addBoxShape();
addSphereShape();
addCylinderShape();
addCapsuleShape();
}

View file

@ -13,14 +13,13 @@
#define hifi_ShapeManagerTests_h
namespace ShapeManagerTests {
void testHashFunctions();
void testShapeAccounting();
void addManyShapes();
void testBoxShape();
void testSphereShape();
void testCylinderShape();
void testCapsuleShape();
void addBoxShape();
void addSphereShape();
void addCylinderShape();
void addCapsuleShape();
void runAllTests();
}
#endif // hifi_VerletShapeTests_h
#endif // hifi_ShapeManagerTests_h

View file

@ -8,13 +8,15 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "ShapeColliderTests.h"
#include "VerletShapeTests.h"
//#include "ShapeColliderTests.h"
//#include "VerletShapeTests.h"
#include "ShapeInfoTests.h"
#include "ShapeManagerTests.h"
int main(int argc, char** argv) {
//ShapeColliderTests::runAllTests();
//VerletShapeTests::runAllTests();
ShapeInfoTests::runAllTests();
ShapeManagerTests::runAllTests();
return 0;
}