Ejemplo n.º 1
0
void BaseUtils_UnitTests()
{
    plogf("Running BaseUtils unit tests");
    BaseUtilTest();
    ByteOrderTests();
    GeomTest();
    TStrTest();
    FileUtilTest();
    VecTest();
    WStrVecTest();
    StrListTest();
    WinUtilTest();
    LogTest();
    BencTest();
    SigSlotTest();
    JsonTest();
    StrFormatTest();
    DictTest();
}
Ejemplo n.º 2
0
void VecTest()
{
    Vec<int> ints;
    utassert(ints.Count() == 0);
    ints.Append(1);
    ints.Push(2);
    ints.InsertAt(0, -1);
    utassert(ints.Count() == 3);
    utassert(ints.At(0) == -1 && ints.At(1) == 1 && ints.At(2) == 2);
    utassert(ints.At(0) == -1 && ints.Last() == 2);
    int last = ints.Pop();
    utassert(last == 2);
    utassert(ints.Count() == 2);
    ints.Push(3);
    ints.RemoveAt(0);
    utassert(ints.Count() == 2);
    utassert(ints.At(0) == 1 && ints.At(1) == 3);
    ints.Reset();
    utassert(ints.Count() == 0);

    for (int i = 0; i < 1000; i++) {
        ints.Push(i);
    }
    utassert(ints.Count() == 1000 && ints.At(500) == 500);
    ints.Remove(500);
    utassert(ints.Count() == 999 && ints.At(500) == 501);
    last = ints.Pop();
    utassert(last == 999);
    ints.Append(last);
    utassert(ints.AtPtr(501) == &ints.At(501));

    for (int& value : ints) {
        utassert(0 <= value && value < 1000);
    }
    utassert(ints.FindEl([](int& value) { return value == 999; }) == 999);
    utassert(ints.FindEl([](int& value) { return value == 500; }) == 0);

    {
        Vec<int> ints2(ints);
        utassert(ints2.Count() == 999);
        utassert(ints.LendData() != ints2.LendData());
        ints.Remove(600);
        utassert(ints.Count() < ints2.Count());
        ints2 = ints;
        utassert(ints2.Count() == 998);
    }

    {
        char buf[2] = {'a', '\0'};
        str::Str<char> v(0);
        for (int i = 0; i < 7; i++) {
            v.Append(buf, 1);
            buf[0] = buf[0] + 1;
        }
        char *s = v.LendData();
        utassert(str::Eq("abcdefg", s));
        utassert(7 == v.Count());
        v.Set("helo");
        utassert(4 == v.Count());
        utassert(str::Eq("helo", v.LendData()));
    }

    {
        str::Str<char> v(128);
        v.Append("boo", 3);
        utassert(str::Eq("boo", v.LendData()));
        utassert(v.Count() == 3);
        v.Append("fop");
        utassert(str::Eq("boofop", v.LendData()));
        utassert(v.Count() == 6);
        v.RemoveAt(2, 3);
        utassert(v.Count() == 3);
        utassert(str::Eq("bop", v.LendData()));
        v.Append('a');
        utassert(v.Count() == 4);
        utassert(str::Eq("bopa", v.LendData()));
        char *s = v.StealData();
        utassert(str::Eq("bopa", s));
        free(s);
        utassert(v.Count() == 0);
    }

    {
        str::Str<char> v(0);
        for (size_t i = 0; i < 32; i++) {
            utassert(v.Count() == i * 6);
            v.Append("lambd", 5);
            if (i % 2 == 0)
                v.Append('a');
            else
                v.Push('a');
        }

        for (size_t i=1; i<=16; i++) {
            v.RemoveAt((16 - i) * 6, 6);
            utassert(v.Count() == (32 - i) * 6);
        }

        v.RemoveAt(0, 6 * 15);
        utassert(v.Count() == 6);
        char *s = v.LendData();
        utassert(str::Eq(s, "lambda"));
        s = v.StealData();
        utassert(str::Eq(s, "lambda"));
        free(s);
        utassert(v.Count() == 0);

        v.Append("lambda");
        utassert(str::Eq(v.LendData(), "lambda"));
        char c = v.Pop();
        utassert(c == 'a');
        utassert(str::Eq(v.LendData(), "lambd"));
    }

    VecTestAppendFmt();

    {
        Vec<PointI *> v;
        srand((unsigned int)time(nullptr));
        for (int i = 0; i < 128; i++) {
            v.Append(new PointI(i, i));
            size_t pos = rand() % v.Count();
            v.InsertAt(pos, new PointI(i, i));
        }
        utassert(v.Count() == 128 * 2);

        while (v.Count() > 64) {
            size_t pos = rand() % v.Count();
            PointI *f = v.At(pos);
            v.Remove(f);
            delete f;
        }
        DeleteVecMembers(v);
    }

    {
        Vec<int> v;
        v.Append(2);
        for (int i = 0; i < 500; i++)
            v.Append(4);
        v.At(250) = 5;
        v.Reverse();
        utassert(v.Count() == 501 && v.At(0) == 4 && v.At(249) == v.At(251) && v.At(250) == 5 && v.At(500) == 2);
        v.Remove(4);
        v.Reverse();
        utassert(v.Count() == 500 && v.At(0) == 2 && v.At(249) == v.At(251) && v.At(250) == 5 && v.At(499) == 4);
    }

    {
        Vec<int> v;
        v.InsertAt(2, 2);
        utassert(v.Count() == 3 && v.At(0) == 0 && v.At(2) == 2);
    }

    WStrVecTest();
    StrListTest();
}