summaryrefslogtreecommitdiffstats
path: root/tools/split-select/Grouper_test.cpp
blob: 7294a86fa8620d72dc9299c2160c040705745e1d (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Grouper.h"

#include "SplitDescription.h"

#include <gtest/gtest.h>
#include <utils/String8.h>
#include <utils/Vector.h>

using namespace android;

namespace split {

class GrouperTest : public ::testing::Test {
protected:
    virtual void SetUp() {
        Vector<SplitDescription> splits;
        addSplit(splits, "en-rUS-sw600dp-hdpi");
        addSplit(splits, "fr-rFR-sw600dp-hdpi");
        addSplit(splits, "fr-rFR-sw600dp-xhdpi");
        addSplit(splits, ":armeabi");
        addSplit(splits, "en-rUS-sw300dp-xhdpi");
        addSplit(splits, "large");
        addSplit(splits, "pl-rPL");
        addSplit(splits, "fr-rCA");
        addSplit(splits, "fr");
        addSplit(splits, "xlarge");
        addSplit(splits, "en-rUS-sw600dp-xhdpi");
        addSplit(splits, "en-rUS-sw300dp-hdpi");
        addSplit(splits, "xxhdpi");
        addSplit(splits, "hdpi");
        addSplit(splits, "de-rDE");
        addSplit(splits, "xhdpi");
        addSplit(splits, ":x86");
        addSplit(splits, "anydpi");
        addSplit(splits, "v7");
        addSplit(splits, "v8");
        addSplit(splits, "sw600dp");
        addSplit(splits, "sw300dp");
        mGroups = groupByMutualExclusivity(splits);
    }

    void addSplit(Vector<SplitDescription>& splits, const char* str);
    void expectHasGroupWithSplits(const char* a);
    void expectHasGroupWithSplits(const char* a, const char* b);
    void expectHasGroupWithSplits(const char* a, const char* b, const char* c);
    void expectHasGroupWithSplits(const char* a, const char* b, const char* c, const char* d);
    void expectHasGroupWithSplits(const Vector<const char*>& expectedStrs);

    Vector<SortedVector<SplitDescription> > mGroups;
};

TEST_F(GrouperTest, shouldHaveCorrectNumberOfGroups) {
    EXPECT_EQ(15u, mGroups.size());
}

TEST_F(GrouperTest, shouldGroupDensities) {
    expectHasGroupWithSplits("en-rUS-sw300dp-hdpi", "en-rUS-sw300dp-xhdpi");
    expectHasGroupWithSplits("en-rUS-sw600dp-hdpi", "en-rUS-sw600dp-xhdpi");
    expectHasGroupWithSplits("fr-rFR-sw600dp-hdpi", "fr-rFR-sw600dp-xhdpi");
    expectHasGroupWithSplits("hdpi", "xhdpi", "xxhdpi", "anydpi");
}

TEST_F(GrouperTest, shouldGroupAbi) {
    expectHasGroupWithSplits(":armeabi", ":x86");
}

TEST_F(GrouperTest, shouldGroupLocale) {
    expectHasGroupWithSplits("pl-rPL");
    expectHasGroupWithSplits("de-rDE");
    expectHasGroupWithSplits("fr");
    expectHasGroupWithSplits("fr-rCA");
}

TEST_F(GrouperTest, shouldGroupEachSplitIntoItsOwnGroup) {
    expectHasGroupWithSplits("large");
    expectHasGroupWithSplits("xlarge");
    expectHasGroupWithSplits("v7");
    expectHasGroupWithSplits("v8");
    expectHasGroupWithSplits("sw600dp");
    expectHasGroupWithSplits("sw300dp");
}

//
// Helper methods
//

void GrouperTest::expectHasGroupWithSplits(const char* a) {
    Vector<const char*> expected;
    expected.add(a);
    expectHasGroupWithSplits(expected);
}

void GrouperTest::expectHasGroupWithSplits(const char* a, const char* b) {
    Vector<const char*> expected;
    expected.add(a);
    expected.add(b);
    expectHasGroupWithSplits(expected);
}

void GrouperTest::expectHasGroupWithSplits(const char* a, const char* b, const char* c) {
    Vector<const char*> expected;
    expected.add(a);
    expected.add(b);
    expected.add(c);
    expectHasGroupWithSplits(expected);
}

void GrouperTest::expectHasGroupWithSplits(const char* a, const char* b, const char* c, const char* d) {
    Vector<const char*> expected;
    expected.add(a);
    expected.add(b);
    expected.add(c);
    expected.add(d);
    expectHasGroupWithSplits(expected);
}

void GrouperTest::expectHasGroupWithSplits(const Vector<const char*>& expectedStrs) {
    Vector<SplitDescription> splits;
    const size_t expectedStrCount = expectedStrs.size();
    for (size_t i = 0; i < expectedStrCount; i++) {
        splits.add();
        if (!SplitDescription::parse(String8(expectedStrs[i]), &splits.editTop())) {
            ADD_FAILURE() << "Failed to parse SplitDescription " << expectedStrs[i];
            return;
        }
    }
    const size_t splitCount = splits.size();

    const size_t groupCount = mGroups.size();
    for (size_t i = 0; i < groupCount; i++) {
        const SortedVector<SplitDescription>& group = mGroups[i];
        if (group.size() != splitCount) {
            continue;
        }

        size_t found = 0;
        for (size_t j = 0; j < splitCount; j++) {
            if (group.indexOf(splits[j]) >= 0) {
                found++;
            }
        }

        if (found == splitCount) {
            return;
        }
    }

    String8 errorMessage("Failed to find expected group [");
    for (size_t i = 0; i < splitCount; i++) {
        if (i != 0) {
            errorMessage.append(", ");
        }
        errorMessage.append(splits[i].toString());
    }
    errorMessage.append("].\nActual:\n");

    for (size_t i = 0; i < groupCount; i++) {
        errorMessage.appendFormat("Group %d:\n", int(i + 1));
        const SortedVector<SplitDescription>& group = mGroups[i];
        for (size_t j = 0; j < group.size(); j++) {
            errorMessage.append("  ");
            errorMessage.append(group[j].toString());
            errorMessage.append("\n");
        }
    }
    ADD_FAILURE() << errorMessage.string();
}

void GrouperTest::addSplit(Vector<SplitDescription>& splits, const char* str) {
    splits.add();
    EXPECT_TRUE(SplitDescription::parse(String8(str), &splits.editTop()));
}

} // namespace split