1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "include/core/SkTypes.h"
9#include "tests/Test.h"
10
11#include "include/utils/SkRandom.h"
12#include "src/core/SkRectPriv.h"
13#include "src/gpu/GrWindowRectangles.h"
14
15static SkIRect next_irect(SkRandom& r) {
16    return {r.nextS(), r.nextS(), r.nextS(), r.nextS()};
17}
18
19DEF_TEST(WindowRectangles, reporter) {
20    SkRandom r;
21
22    SkIRect windowData[GrWindowRectangles::kMaxWindows];
23    for (int i = 0; i < GrWindowRectangles::kMaxWindows; ++i) {
24        windowData[i] = next_irect(r);
25    }
26
27    GrWindowRectangles wr;
28    for (int i = 0; i < GrWindowRectangles::kMaxWindows - 1; ++i) {
29        REPORTER_ASSERT(reporter, wr.count() == i);
30        REPORTER_ASSERT(reporter, !memcmp(wr.data(), windowData, i * sizeof(SkIRect)));
31
32        GrWindowRectangles wr2(wr);
33        REPORTER_ASSERT(reporter, wr2 == wr);
34        REPORTER_ASSERT(reporter, wr2.count() == wr.count());
35        REPORTER_ASSERT(reporter, !memcmp(wr2.data(), wr.data(), i * sizeof(SkIRect)));
36
37        wr.addWindow(windowData[i]);
38    }
39
40    SkASSERT(wr.count() == GrWindowRectangles::kMaxWindows - 1);
41    {
42        GrWindowRectangles A(wr), B(wr);
43        REPORTER_ASSERT(reporter, B == A);
44        REPORTER_ASSERT(reporter, B.data() == A.data()); // Should use copy-on-write.
45
46        A.addWindow(windowData[GrWindowRectangles::kMaxWindows - 1]);
47        REPORTER_ASSERT(reporter, B.data() != A.data());
48        REPORTER_ASSERT(reporter, B != A);
49
50        B.addWindow(SkRectPriv::MakeILarge());
51        REPORTER_ASSERT(reporter, B != A);
52
53        for (int i = 0; i < GrWindowRectangles::kMaxWindows - 1; i++) {
54            REPORTER_ASSERT(reporter, A.data()[i] == windowData[i]);
55            REPORTER_ASSERT(reporter, B.data()[i] == windowData[i]);
56        }
57        REPORTER_ASSERT(reporter, A.data()[GrWindowRectangles::kMaxWindows - 1]
58                             == windowData[GrWindowRectangles::kMaxWindows - 1]);
59        REPORTER_ASSERT(reporter, B.data()[GrWindowRectangles::kMaxWindows - 1]
60                             == SkRectPriv::MakeILarge());
61    }
62    {
63        GrWindowRectangles A(wr), B(wr);
64        REPORTER_ASSERT(reporter, B == A);
65        REPORTER_ASSERT(reporter, B.data() == A.data()); // Should use copy-on-write.
66
67        A.addWindow(windowData[GrWindowRectangles::kMaxWindows - 1]);
68        B.addWindow(windowData[GrWindowRectangles::kMaxWindows - 1]);
69        REPORTER_ASSERT(reporter, B == A);
70        REPORTER_ASSERT(reporter, B.data() != A.data());
71
72        for (int i = 0; i < GrWindowRectangles::kMaxWindows; i++) {
73            REPORTER_ASSERT(reporter, B.data()[i] ==   A.data()[i]);
74            REPORTER_ASSERT(reporter, A.data()[i] == windowData[i]);
75        }
76    }
77}
78