1 // Copyright 2018 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/fpdfapi/page/cpdf_pageobjectholder.h"
6
7 #include <math.h>
8
9 #include <algorithm>
10 #include <array>
11 #include <limits>
12 #include <vector>
13
14 #include "core/fxcrt/fx_extension.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
SafeCompare(const float & x,const float & y)17 bool SafeCompare(const float& x, const float& y) {
18 return FXSYS_SafeLT(x, y);
19 }
20
21 // See https://crbug.com/852273
TEST(CPDFPageObjectHolder,GraphicsDataAsKey)22 TEST(CPDFPageObjectHolder, GraphicsDataAsKey) {
23 const float fMin = std::numeric_limits<float>::min();
24 const float fMax = std::numeric_limits<float>::max();
25 const float fInf = std::numeric_limits<float>::infinity();
26 const float fNan = std::numeric_limits<float>::quiet_NaN();
27
28 // Verify self-comparisions.
29 for (float c1 : {fMin, 1.0f, 2.0f, fMax, fInf, fNan}) {
30 for (float c2 : {fMin, 1.0f, 2.0f, fMax, fInf, fNan}) {
31 for (BlendMode c3 : {BlendMode::kMultiply, BlendMode::kScreen})
32 EXPECT_FALSE(GraphicsData({c1, c2, c3}) < GraphicsData({c1, c2, c3}));
33 }
34 }
35
36 // Validate the documented sort order.
37 std::vector<float> data = {fMax, fInf, fNan, fMin};
38 std::sort(data.begin(), data.end(), SafeCompare);
39 EXPECT_EQ(data[0], fMin);
40 EXPECT_EQ(data[1], fMax);
41 EXPECT_EQ(data[2], fInf);
42 EXPECT_EQ(isnan(data[3]), isnan(fNan));
43
44 std::map<GraphicsData, int> graphics_map;
45
46 // Insert in reverse index permuted order.
47 size_t x = 0;
48 for (BlendMode c3 : {BlendMode::kScreen, BlendMode::kMultiply}) {
49 for (float c2 : {fNan, fInf, fMax, 2.0f, 1.0f, fMin}) {
50 for (float c1 : {fNan, fInf, fMax, 2.0f, 1.0f, fMin}) {
51 graphics_map[{c1, c2, c3}] = x++;
52 }
53 }
54 }
55 EXPECT_EQ(72u, x);
56 EXPECT_EQ(72u, graphics_map.size());
57
58 // clang-format off
59 std::array<const int, 72> expected = {
60 71, 35, 65, 29, 59, 23, 53, 17, 47, 11, 41, 5,
61 70, 34, 64, 28, 58, 22, 52, 16, 46, 10, 40, 4,
62 69, 33, 63, 27, 57, 21, 51, 15, 45, 9, 39, 3,
63 68, 32, 62, 26, 56, 20, 50, 14, 44, 8, 38, 2,
64 67, 31, 61, 25, 55, 19, 49, 13, 43, 7, 37, 1,
65 66, 30, 60, 24, 54, 18, 48, 12, 42, 6, 36, 0
66 };
67 // clang-format on
68
69 x = 0;
70 for (const auto& item : graphics_map) {
71 EXPECT_EQ(expected[x], item.second) << " for position " << x;
72 ++x;
73 }
74 EXPECT_EQ(72u, x);
75
76 // Erase in forward index permuted order.
77 for (BlendMode c3 : {BlendMode::kMultiply, BlendMode::kScreen}) {
78 for (float c2 : {fMin, 1.0f, 2.0f, fMax, fInf, fNan}) {
79 for (float c1 : {fMin, 1.0f, 2.0f, fMax, fInf, fNan})
80 graphics_map.erase({c1, c2, c3});
81 }
82 }
83 EXPECT_EQ(0u, graphics_map.size());
84 }
85