aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/HashTest.cpp
blob: 24032a5ec5652dc6f3d150fe175414b24af1f72a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "SkChecksum.h"
#include "SkString.h"
#include "SkTHash.h"
#include "Test.h"

namespace { uint32_t hash_int(int k) { return SkChecksum::Mix(k); } }

static void set_negative_key(int key, double* d) { *d = -key; }

DEF_TEST(HashMap, r) {
    SkTHashMap<int, double, hash_int> map;

    map.set(3, 4.0);
    REPORTER_ASSERT(r, map.count() == 1);

    double* found = map.find(3);
    REPORTER_ASSERT(r, found);
    REPORTER_ASSERT(r, *found == 4.0);

    map.foreach(set_negative_key);
    found = map.find(3);
    REPORTER_ASSERT(r, found);
    REPORTER_ASSERT(r, *found == -3.0);

    REPORTER_ASSERT(r, !map.find(2));

    const int N = 20;

    for (int i = 0; i < N; i++) {
        map.set(i, 2.0*i);
    }
    for (int i = 0; i < N; i++) {
        double* found = map.find(i);;
        REPORTER_ASSERT(r, found);
        REPORTER_ASSERT(r, *found == i*2.0);
    }
    for (int i = N; i < 2*N; i++) {
        REPORTER_ASSERT(r, !map.find(i));
    }

    REPORTER_ASSERT(r, map.count() == N);
}

namespace { uint32_t hash_string(SkString s) { return s.size(); } }

DEF_TEST(HashSet, r) {
    SkTHashSet<SkString, hash_string> set;

    set.add(SkString("Hello"));
    set.add(SkString("World"));

    REPORTER_ASSERT(r, set.count() == 2);

    REPORTER_ASSERT(r, set.contains(SkString("Hello")));
    REPORTER_ASSERT(r, set.contains(SkString("World")));
    REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
}