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