2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2009-02-27 22:06:06 +00:00
|
|
|
#include "Test.h"
|
2013-12-12 21:11:12 +00:00
|
|
|
#include "TestClassDef.h"
|
2013-12-30 14:40:38 +00:00
|
|
|
#include "Sk64.h"
|
2009-02-27 22:06:06 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
struct BoolTable {
|
|
|
|
int8_t zero, pos, neg, toBool, sign;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void bool_table_test(skiatest::Reporter* reporter,
|
|
|
|
const Sk64& a, const BoolTable& table)
|
|
|
|
{
|
|
|
|
REPORTER_ASSERT(reporter, a.isZero() != a.nonZero());
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !a.isZero() == !table.zero);
|
|
|
|
REPORTER_ASSERT(reporter, !a.isPos() == !table.pos);
|
|
|
|
REPORTER_ASSERT(reporter, !a.isNeg() == !table.neg);
|
|
|
|
REPORTER_ASSERT(reporter, a.getSign() == table.sign);
|
|
|
|
}
|
|
|
|
|
2013-12-20 14:24:21 +00:00
|
|
|
void Sk64::UnitTestWithReporter(void* reporterParam) {
|
|
|
|
skiatest::Reporter* reporter = (skiatest::Reporter*)reporterParam;
|
2013-08-15 13:43:23 +00:00
|
|
|
|
2009-02-27 22:06:06 +00:00
|
|
|
enum BoolTests {
|
|
|
|
kZero_BoolTest,
|
|
|
|
kPos_BoolTest,
|
|
|
|
kNeg_BoolTest
|
|
|
|
};
|
|
|
|
static const BoolTable gBoolTable[] = {
|
|
|
|
{ 1, 0, 0, 0, 0 },
|
|
|
|
{ 0, 1, 0, 1, 1 },
|
|
|
|
{ 0, 0, 1, 1, -1 }
|
|
|
|
};
|
|
|
|
|
|
|
|
Sk64 a, b, c;
|
|
|
|
|
|
|
|
a.fHi = a.fLo = 0;
|
|
|
|
b.set(0);
|
|
|
|
c.setZero();
|
|
|
|
REPORTER_ASSERT(reporter, a == b);
|
|
|
|
REPORTER_ASSERT(reporter, a == c);
|
|
|
|
bool_table_test(reporter, a, gBoolTable[kZero_BoolTest]);
|
|
|
|
|
|
|
|
a.fHi = 0; a.fLo = 5;
|
|
|
|
b.set(5);
|
|
|
|
REPORTER_ASSERT(reporter, a == b);
|
|
|
|
REPORTER_ASSERT(reporter, a.is32() && a.get32() == 5 && !a.is64());
|
|
|
|
bool_table_test(reporter, a, gBoolTable[kPos_BoolTest]);
|
|
|
|
|
|
|
|
a.fHi = -1; a.fLo = (uint32_t)-5;
|
|
|
|
b.set(-5);
|
|
|
|
REPORTER_ASSERT(reporter, a == b);
|
|
|
|
REPORTER_ASSERT(reporter, a.is32() && a.get32() == -5 && !a.is64());
|
|
|
|
bool_table_test(reporter, a, gBoolTable[kNeg_BoolTest]);
|
|
|
|
|
|
|
|
a.setZero();
|
|
|
|
b.set(6);
|
|
|
|
c.set(-6);
|
|
|
|
REPORTER_ASSERT(reporter, a != b && b != c && a != c);
|
|
|
|
REPORTER_ASSERT(reporter, !(a == b) && !(a == b) && !(a == b));
|
|
|
|
REPORTER_ASSERT(reporter, a < b && b > a && a <= b && b >= a);
|
|
|
|
REPORTER_ASSERT(reporter, c < a && a > c && c <= a && a >= c);
|
|
|
|
REPORTER_ASSERT(reporter, c < b && b > c && c <= b && b >= c);
|
|
|
|
|
|
|
|
// Now test add/sub
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2009-02-27 22:06:06 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 1000; i++)
|
|
|
|
{
|
|
|
|
int aa = rand.nextS() >> 1;
|
|
|
|
int bb = rand.nextS() >> 1;
|
|
|
|
a.set(aa);
|
|
|
|
b.set(bb);
|
|
|
|
REPORTER_ASSERT(reporter, a.get32() == aa && b.get32() == bb);
|
|
|
|
c = a; c.add(bb);
|
|
|
|
REPORTER_ASSERT(reporter, c.get32() == aa + bb);
|
|
|
|
c = a; c.add(-bb);
|
|
|
|
REPORTER_ASSERT(reporter, c.get32() == aa - bb);
|
|
|
|
c = a; c.add(b);
|
|
|
|
REPORTER_ASSERT(reporter, c.get32() == aa + bb);
|
|
|
|
c = a; c.sub(b);
|
|
|
|
REPORTER_ASSERT(reporter, c.get32() == aa - bb);
|
|
|
|
}
|
2013-08-15 13:43:23 +00:00
|
|
|
|
2013-12-20 14:24:21 +00:00
|
|
|
for (i = 0; i < 1000; i++) {
|
2013-12-30 14:40:38 +00:00
|
|
|
a.set64(rand.next64());
|
|
|
|
b.set64(rand.next64());
|
2013-08-15 13:43:23 +00:00
|
|
|
|
|
|
|
if (!(i & 3)) // want to explicitly test these cases
|
|
|
|
{
|
|
|
|
a.fLo = 0;
|
|
|
|
b.fLo = 0;
|
|
|
|
}
|
|
|
|
else if (!(i & 7)) // want to explicitly test these cases
|
|
|
|
{
|
|
|
|
a.fHi = 0;
|
|
|
|
b.fHi = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-20 14:24:21 +00:00
|
|
|
int64_t aa = a.as64();
|
|
|
|
int64_t bb = b.as64();
|
2013-08-15 13:43:23 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, (a < b) == (aa < bb));
|
|
|
|
REPORTER_ASSERT(reporter, (a <= b) == (aa <= bb));
|
|
|
|
REPORTER_ASSERT(reporter, (a > b) == (aa > bb));
|
|
|
|
REPORTER_ASSERT(reporter, (a >= b) == (aa >= bb));
|
|
|
|
REPORTER_ASSERT(reporter, (a == b) == (aa == bb));
|
|
|
|
REPORTER_ASSERT(reporter, (a != b) == (aa != bb));
|
|
|
|
|
|
|
|
c = a; c.add(b);
|
2013-12-20 14:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, c.as64() == aa + bb);
|
2013-08-15 13:43:23 +00:00
|
|
|
c = a; c.sub(b);
|
2013-12-20 14:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, c.as64() == aa - bb);
|
2013-08-15 13:43:23 +00:00
|
|
|
c = a; c.rsub(b);
|
2013-12-20 14:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, c.as64() == bb - aa);
|
2013-08-15 13:43:23 +00:00
|
|
|
c = a; c.negate();
|
2013-12-20 14:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, c.as64() == -aa);
|
2013-08-15 13:43:23 +00:00
|
|
|
|
|
|
|
int bits = rand.nextU() & 63;
|
|
|
|
c = a; c.shiftLeft(bits);
|
2013-12-20 14:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, c.as64() == (aa << bits));
|
2013-08-15 13:43:23 +00:00
|
|
|
c = a; c.shiftRight(bits);
|
2013-12-20 14:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, c.as64() == (aa >> bits));
|
2013-08-15 13:43:23 +00:00
|
|
|
c = a; c.roundRight(bits);
|
|
|
|
|
2013-12-20 14:24:21 +00:00
|
|
|
int64_t tmp;
|
2013-08-15 13:43:23 +00:00
|
|
|
|
|
|
|
tmp = aa;
|
|
|
|
if (bits > 0)
|
2013-12-20 14:24:21 +00:00
|
|
|
tmp += (int64_t)1 << (bits - 1);
|
|
|
|
REPORTER_ASSERT(reporter, c.as64() == (tmp >> bits));
|
2013-08-15 13:43:23 +00:00
|
|
|
|
|
|
|
c.setMul(a.fHi, b.fHi);
|
2013-12-20 14:24:21 +00:00
|
|
|
tmp = (int64_t)a.fHi * b.fHi;
|
|
|
|
REPORTER_ASSERT(reporter, c.as64() == tmp);
|
2013-08-15 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 100000; i++)
|
|
|
|
{
|
|
|
|
Sk64 wide;
|
|
|
|
int32_t denom = rand.nextS();
|
|
|
|
|
|
|
|
while (denom == 0)
|
|
|
|
denom = rand.nextS();
|
|
|
|
wide.setMul(rand.nextS(), rand.nextS());
|
2013-12-20 14:24:21 +00:00
|
|
|
int64_t check = wide.getLongLong();
|
2013-08-15 13:43:23 +00:00
|
|
|
|
|
|
|
wide.div(denom, Sk64::kTrunc_DivOption);
|
|
|
|
check /= denom;
|
2013-12-20 14:24:21 +00:00
|
|
|
int64_t w = wide.getLongLong();
|
2013-08-15 13:43:23 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, check == w);
|
|
|
|
|
|
|
|
wide.setMul(rand.nextS(), rand.nextS());
|
|
|
|
wide.abs();
|
|
|
|
denom = wide.getSqrt();
|
|
|
|
int32_t ck = (int32_t)sqrt((double)wide.getLongLong());
|
|
|
|
int diff = denom - ck;
|
|
|
|
REPORTER_ASSERT(reporter, SkAbs32(diff) <= 1);
|
|
|
|
}
|
2013-12-20 14:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Sk64Test, reporter) {
|
|
|
|
Sk64::UnitTestWithReporter(reporter);
|
2009-02-27 22:06:06 +00:00
|
|
|
}
|