Merge pull request #9281 from AndrewMeadows/unit-test-cleanup

fix bitrot in unit tests
This commit is contained in:
Andrew Meadows 2016-12-29 16:21:35 -08:00 committed by GitHub
commit ff19bdceaa
8 changed files with 49 additions and 62 deletions

View file

@ -370,7 +370,7 @@ glm::quat glmExtractRotation(const glm::mat4& matrix) {
glm::vec3 extractScale(const glm::mat4& matrix) { glm::vec3 extractScale(const glm::mat4& matrix) {
glm::mat3 m(matrix); glm::mat3 m(matrix);
float det = glm::determinant(m); float det = glm::determinant(m);
if (det < 0) { if (det < 0.0f) {
// left handed matrix, flip sign to compensate. // left handed matrix, flip sign to compensate.
return glm::vec3(-glm::length(m[0]), glm::length(m[1]), glm::length(m[2])); return glm::vec3(-glm::length(m[0]), glm::length(m[1]), glm::length(m[2]));
} else { } else {

View file

@ -36,7 +36,7 @@ void AudioRingBufferTests::runAllTests() {
int readIndexAt; int readIndexAt;
AudioRingBuffer ringBuffer(10, false, 10); // makes buffer of 100 int16_t samples AudioRingBuffer ringBuffer(10, 10); // makes buffer of 100 int16_t samples
for (int T = 0; T < 300; T++) { for (int T = 0; T < 300; T++) {
writeIndexAt = 0; writeIndexAt = 0;

View file

@ -12,29 +12,29 @@
#include "PacketTests.h" #include "PacketTests.h"
#include "../QTestExtensions.h" #include "../QTestExtensions.h"
#include <udt/Packet.h> #include <NLPacket.h>
QTEST_MAIN(PacketTests) QTEST_MAIN(PacketTests)
std::unique_ptr<Packet> copyToReadPacket(std::unique_ptr<Packet>& packet) { std::unique_ptr<NLPacket> copyToReadPacket(std::unique_ptr<NLPacket>& packet) {
auto size = packet->getDataSize(); auto size = packet->getDataSize();
auto data = std::unique_ptr<char[]>(new char[size]); auto data = std::unique_ptr<char[]>(new char[size]);
memcpy(data.get(), packet->getData(), size); memcpy(data.get(), packet->getData(), size);
return Packet::fromReceivedPacket(std::move(data), size, HifiSockAddr()); return NLPacket::fromReceivedPacket(std::move(data), size, HifiSockAddr());
} }
void PacketTests::emptyPacketTest() { void PacketTests::emptyPacketTest() {
auto packet = Packet::create(PacketType::Unknown); auto packet = NLPacket::create(PacketType::Unknown);
QCOMPARE(packet->getType(), PacketType::Unknown); QCOMPARE(packet->getType(), PacketType::Unknown);
QCOMPARE(packet->getPayloadSize(), 0); QCOMPARE(packet->getPayloadSize(), 0);
QCOMPARE(packet->getDataSize(), packet->totalHeadersSize()); QCOMPARE(packet->getDataSize(), NLPacket::totalHeaderSize(packet->getType()));
QCOMPARE(packet->bytesLeftToRead(), 0); QCOMPARE(packet->bytesLeftToRead(), 0);
QCOMPARE(packet->bytesAvailableForWrite(), packet->getPayloadCapacity()); QCOMPARE(packet->bytesAvailableForWrite(), packet->getPayloadCapacity());
} }
void PacketTests::packetTypeTest() { void PacketTests::packetTypeTest() {
auto packet = Packet::create(PacketType::EntityAdd); auto packet = NLPacket::create(PacketType::EntityAdd);
QCOMPARE(packet->getType(), PacketType::EntityAdd); QCOMPARE(packet->getType(), PacketType::EntityAdd);
@ -46,7 +46,7 @@ void PacketTests::packetTypeTest() {
} }
void PacketTests::writeTest() { void PacketTests::writeTest() {
auto packet = Packet::create(PacketType::Unknown); auto packet = NLPacket::create(PacketType::Unknown);
QCOMPARE(packet->getPayloadSize(), 0); QCOMPARE(packet->getPayloadSize(), 0);
@ -62,7 +62,7 @@ void PacketTests::writeTest() {
void PacketTests::readTest() { void PacketTests::readTest() {
// Test reads for several different size packets // Test reads for several different size packets
for (int i = 1; i < 4; i++) { for (int i = 1; i < 4; i++) {
auto packet = Packet::create(PacketType::Unknown); auto packet = NLPacket::create(PacketType::Unknown);
auto size = packet->getPayloadCapacity(); auto size = packet->getPayloadCapacity();
size /= i; size /= i;
@ -91,7 +91,7 @@ void PacketTests::readTest() {
} }
void PacketTests::writePastCapacityTest() { void PacketTests::writePastCapacityTest() {
auto packet = Packet::create(PacketType::Unknown); auto packet = NLPacket::create(PacketType::Unknown);
auto size = packet->getPayloadCapacity(); auto size = packet->getPayloadCapacity();
char* data = new char[size]; char* data = new char[size];
@ -111,14 +111,14 @@ void PacketTests::writePastCapacityTest() {
QCOMPARE(packet->bytesAvailableForWrite(), 0); QCOMPARE(packet->bytesAvailableForWrite(), 0);
QCOMPARE(packet->getPayloadSize(), size); QCOMPARE(packet->getPayloadSize(), size);
QCOMPARE(Packet::PACKET_WRITE_ERROR, packet->write("data")); QCOMPARE(NLPacket::PACKET_WRITE_ERROR, packet->write("data")); // asserts in DEBUG
// Packet::write() shouldn't allow the caller to write if no space is left // NLPacket::write() shouldn't allow the caller to write if no space is left
QCOMPARE(packet->getPayloadSize(), size); QCOMPARE(packet->getPayloadSize(), size);
} }
void PacketTests::primitiveTest() { void PacketTests::primitiveTest() {
auto packet = Packet::create(PacketType::Unknown); auto packet = NLPacket::create(PacketType::Unknown);
int value1 = 5; int value1 = 5;
char value2 = 10; char value2 = 10;

View file

@ -37,25 +37,10 @@ void ResourceTests::initTestCase() {
static QSharedPointer<Resource> resource; static QSharedPointer<Resource> resource;
static bool waitForSignal(QObject *sender, const char *signal, int timeout = 1000) {
QEventLoop loop;
QTimer timer;
timer.setInterval(timeout);
timer.setSingleShot(true);
loop.connect(sender, signal, SLOT(quit()));
loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
timer.start();
loop.exec();
return timer.isActive();
}
void ResourceTests::downloadFirst() { void ResourceTests::downloadFirst() {
// download the Mery fst file // download the Mery fst file
QUrl meryUrl = QUrl("http://hifi-public.s3.amazonaws.com/marketplace/contents/e21c0b95-e502-4d15-8c41-ea2fc40f1125/3585ddf674869a67d31d5964f7b52de1.fst"); QUrl meryUrl = QUrl("http://hifi-public.s3.amazonaws.com/marketplace/contents/e21c0b95-e502-4d15-8c41-ea2fc40f1125/3585ddf674869a67d31d5964f7b52de1.fst");
resource = QSharedPointer<Resource>::create(meryUrl, false); resource = QSharedPointer<Resource>::create(meryUrl);
resource->setSelf(resource); resource->setSelf(resource);
const int timeout = 1000; const int timeout = 1000;
@ -64,9 +49,9 @@ void ResourceTests::downloadFirst() {
timer.setInterval(timeout); timer.setInterval(timeout);
timer.setSingleShot(true); timer.setSingleShot(true);
loop.connect(resource, SIGNAL(loaded(QNetworkReply&)), SLOT(quit())); connect(resource.data(), &Resource::loaded, &loop, &QEventLoop::quit);
loop.connect(resource, SIGNAL(failed(QNetworkReply::NetworkError)), SLOT(quit())); connect(resource.data(), &Resource::failed, &loop, &QEventLoop::quit);
loop.connect(&timer, SIGNAL(timeout()), SLOT(quit())); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
timer.start(); timer.start();
resource->ensureLoading(); resource->ensureLoading();
@ -76,10 +61,9 @@ void ResourceTests::downloadFirst() {
} }
void ResourceTests::downloadAgain() { void ResourceTests::downloadAgain() {
// download the Mery fst file // download the Mery fst file
QUrl meryUrl = QUrl("http://hifi-public.s3.amazonaws.com/marketplace/contents/e21c0b95-e502-4d15-8c41-ea2fc40f1125/3585ddf674869a67d31d5964f7b52de1.fst"); QUrl meryUrl = QUrl("http://hifi-public.s3.amazonaws.com/marketplace/contents/e21c0b95-e502-4d15-8c41-ea2fc40f1125/3585ddf674869a67d31d5964f7b52de1.fst");
resource = QSharedPointer<Resource>::create(meryUrl, false); resource = QSharedPointer<Resource>::create(meryUrl);
resource->setSelf(resource); resource->setSelf(resource);
const int timeout = 1000; const int timeout = 1000;
@ -88,14 +72,13 @@ void ResourceTests::downloadAgain() {
timer.setInterval(timeout); timer.setInterval(timeout);
timer.setSingleShot(true); timer.setSingleShot(true);
loop.connect(resource, SIGNAL(loaded(QNetworkReply&)), SLOT(quit())); connect(resource.data(), &Resource::loaded, &loop, &QEventLoop::quit);
loop.connect(resource, SIGNAL(failed(QNetworkReply::NetworkError)), SLOT(quit())); connect(resource.data(), &Resource::failed, &loop, &QEventLoop::quit);
loop.connect(&timer, SIGNAL(timeout()), SLOT(quit())); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
timer.start(); timer.start();
resource->ensureLoading(); resource->ensureLoading();
loop.exec(); loop.exec();
QVERIFY(resource->isLoaded()); QVERIFY(resource->isLoaded());
} }

View file

@ -277,7 +277,7 @@ void GeometryUtilTests::testSphereCapsulePenetration() {
glm::vec3 capsuleEnd(0.0f, 10.0f, 0.0f); glm::vec3 capsuleEnd(0.0f, 10.0f, 0.0f);
float capsuleRadius = 1.0f; float capsuleRadius = 1.0f;
glm::vec3 penetration(glm::vec3::_null); glm::vec3 penetration(0.0f);
bool hit = findSphereCapsulePenetration(sphereCenter, sphereRadius, capsuleStart, capsuleEnd, capsuleRadius, penetration); bool hit = findSphereCapsulePenetration(sphereCenter, sphereRadius, capsuleStart, capsuleEnd, capsuleRadius, penetration);
QCOMPARE(hit, true); QCOMPARE(hit, true);
QCOMPARE_WITH_ABS_ERROR(penetration, glm::vec3(-0.5f, 0.0f, 0.0f), EPSILON); QCOMPARE_WITH_ABS_ERROR(penetration, glm::vec3(-0.5f, 0.0f, 0.0f), EPSILON);

View file

@ -39,19 +39,21 @@ void MovingPercentileTests::testRunningMedian() {
} }
float MovingPercentileTests::random() { int64_t MovingPercentileTests::random() {
return rand() / (float)RAND_MAX; return ((int64_t) rand() << 48) ^
((int64_t) rand() << 32) ^
((int64_t) rand() << 16) ^
((int64_t) rand());
} }
void MovingPercentileTests::testRunningMinForN (int n) { void MovingPercentileTests::testRunningMinForN (int n) {
// Stores the last n samples // Stores the last n samples
QQueue<float> samples; QQueue<int64_t> samples;
MovingPercentile movingMin (n, 0.0f); MovingPercentile movingMin (n, 0.0f);
for (int s = 0; s < 3 * n; ++s) { for (int s = 0; s < 3 * n; ++s) {
float sample = random(); int64_t sample = random();
samples.push_back(sample); samples.push_back(sample);
if (samples.size() > n) if (samples.size() > n)
@ -64,30 +66,32 @@ void MovingPercentileTests::testRunningMinForN (int n) {
movingMin.updatePercentile(sample); movingMin.updatePercentile(sample);
// Calculate the minimum of the moving samples // Calculate the minimum of the moving samples
float expectedMin = std::numeric_limits<float>::max(); int64_t expectedMin = std::numeric_limits<int64_t>::max();
int prevSize = samples.size(); int prevSize = samples.size();
for (auto val : samples) for (auto val : samples) {
expectedMin = std::min(val, expectedMin); expectedMin = std::min(val, expectedMin);
}
QCOMPARE(samples.size(), prevSize); QCOMPARE(samples.size(), prevSize);
QCOMPARE(movingMin.getValueAtPercentile(), expectedMin); QVERIFY(movingMin.getValueAtPercentile() - expectedMin == 0L);
} }
} }
void MovingPercentileTests::testRunningMaxForN (int n) { void MovingPercentileTests::testRunningMaxForN (int n) {
// Stores the last n samples // Stores the last n samples
QQueue<float> samples; QQueue<int64_t> samples;
MovingPercentile movingMax (n, 1.0f); MovingPercentile movingMax (n, 1.0f);
for (int s = 0; s < 10000; ++s) { for (int s = 0; s < 10000; ++s) {
float sample = random(); int64_t sample = random();
samples.push_back(sample); samples.push_back(sample);
if (samples.size() > n) if (samples.size() > n) {
samples.pop_front(); samples.pop_front();
}
if (samples.size() == 0) { if (samples.size() == 0) {
QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n); QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n);
@ -96,22 +100,22 @@ void MovingPercentileTests::testRunningMaxForN (int n) {
movingMax.updatePercentile(sample); movingMax.updatePercentile(sample);
// Calculate the maximum of the moving samples // Calculate the maximum of the moving samples
float expectedMax = std::numeric_limits<float>::min(); int64_t expectedMax = std::numeric_limits<int64_t>::min();
for (auto val : samples) for (auto val : samples)
expectedMax = std::max(val, expectedMax); expectedMax = std::max(val, expectedMax);
QCOMPARE(movingMax.getValueAtPercentile(), expectedMax); QVERIFY(movingMax.getValueAtPercentile() - expectedMax == 0L);
} }
} }
void MovingPercentileTests::testRunningMedianForN (int n) { void MovingPercentileTests::testRunningMedianForN (int n) {
// Stores the last n samples // Stores the last n samples
QQueue<float> samples; QQueue<int64_t> samples;
MovingPercentile movingMedian (n, 0.5f); MovingPercentile movingMedian (n, 0.5f);
for (int s = 0; s < 10000; ++s) { for (int s = 0; s < 10000; ++s) {
float sample = random(); int64_t sample = random();
samples.push_back(sample); samples.push_back(sample);
if (samples.size() > n) if (samples.size() > n)

View file

@ -25,7 +25,7 @@ private slots:
private: private:
// Utilities and helper functions // Utilities and helper functions
float random(); int64_t random();
void testRunningMinForN (int n); void testRunningMinForN (int n);
void testRunningMaxForN (int n); void testRunningMaxForN (int n);
void testRunningMedianForN (int n); void testRunningMedianForN (int n);

View file

@ -28,7 +28,7 @@ void TraceTests::testTraceSerialization() {
auto start = usecTimestampNow(); auto start = usecTimestampNow();
PROFILE_RANGE(test, "TestEvent") PROFILE_RANGE(test, "TestEvent")
for (size_t i = 0; i < 10000; ++i) { for (size_t i = 0; i < 10000; ++i) {
SAMPLE_PROFILE_COUNTER(0.1f, test, "TestCounter", { { "i", i } }) SAMPLE_PROFILE_COUNTER(0.1f, test, "TestCounter", { { "i", (int)i } })
} }
auto duration = usecTimestampNow() - start; auto duration = usecTimestampNow() - start;
duration /= USECS_PER_MSEC; duration /= USECS_PER_MSEC;