[M108 Migration][HBBTV] Implement ewk_context_register_jsplugin_mime_types API
[platform/framework/web/chromium-efl.git] / courgette / adjustment_method_unittest.cc
1 // Copyright 2011 The Chromium 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 <memory>
6 #include <sstream>
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/memory/ptr_util.h"
13 #include "courgette/assembly_program.h"
14 #include "courgette/courgette.h"
15 #include "courgette/encoded_program.h"
16 #include "courgette/image_utils.h"
17 #include "courgette/streams.h"
18
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace courgette {
22
23 namespace {
24
25 class AdjustmentMethodTest : public testing::Test {
26  public:
27   void Test1() const;
28
29  private:
30   void SetUp() {
31   }
32
33   void TearDown() {
34   }
35
36   // Returns one of two similar simple programs. These differ only in Label
37   // assignment, so it is possible to make them look identical.
38   std::unique_ptr<AssemblyProgram> MakeProgram(int kind) const {
39     auto prog = std::make_unique<AssemblyProgram>(EXE_WIN_32_X86, 0x00400000);
40
41     RVA kRvaA = 0x00410000;
42     RVA kRvaB = 0x00410004;
43
44     std::vector<RVA> abs32_rvas;
45     abs32_rvas.push_back(kRvaA);
46     abs32_rvas.push_back(kRvaB);
47     std::vector<RVA> rel32_rvas;  // Stub.
48
49     TrivialRvaVisitor abs32_visitor(abs32_rvas);
50     TrivialRvaVisitor rel32_visitor(rel32_rvas);
51     prog->PrecomputeLabels(&abs32_visitor, &rel32_visitor);
52
53     Label* labelA = prog->FindAbs32Label(kRvaA);
54     Label* labelB = prog->FindAbs32Label(kRvaB);
55
56     InstructionGenerator gen = base::BindRepeating(
57         [](Label* labelA, Label* labelB,
58            InstructionReceptor* receptor) -> CheckBool {
59           EXPECT_TRUE(receptor->EmitAbs32(labelA));
60           EXPECT_TRUE(receptor->EmitAbs32(labelA));
61           EXPECT_TRUE(receptor->EmitAbs32(labelB));
62           EXPECT_TRUE(receptor->EmitAbs32(labelA));
63           EXPECT_TRUE(receptor->EmitAbs32(labelA));
64           EXPECT_TRUE(receptor->EmitAbs32(labelB));
65           return true;
66         },
67         labelA, labelB);
68
69     EXPECT_TRUE(prog->AnnotateLabels(gen));
70     EXPECT_EQ(6U, prog->abs32_label_annotations().size());
71     EXPECT_EQ(0U, prog->rel32_label_annotations().size());
72
73     if (kind == 0) {
74       labelA->index_ = 0;
75       labelB->index_ = 1;
76     } else {
77       labelA->index_ = 1;
78       labelB->index_ = 0;
79     }
80     prog->AssignRemainingIndexes();
81
82     return prog;
83   }
84
85   std::unique_ptr<AssemblyProgram> MakeProgramA() const {
86     return MakeProgram(0);
87   }
88   std::unique_ptr<AssemblyProgram> MakeProgramB() const {
89     return MakeProgram(1);
90   }
91
92   // Returns a string that is the serialized version of |program| annotations.
93   std::string Serialize(AssemblyProgram* program) const {
94     std::ostringstream oss;
95     for (const Label* label : program->abs32_label_annotations())
96       oss << "(" << label->rva_ << "," << label->index_ << ")";
97     oss << ";";
98     for (const Label* label : program->rel32_label_annotations())
99       oss << "(" << label->rva_ << "," << label->index_ << ")";
100
101     EXPECT_GT(oss.str().length(), 1U);  // Ensure results are non-trivial.
102     return oss.str();
103   }
104 };
105
106 void AdjustmentMethodTest::Test1() const {
107   std::unique_ptr<AssemblyProgram> prog1 = MakeProgramA();
108   std::unique_ptr<AssemblyProgram> prog2 = MakeProgramB();
109   std::string s1 = Serialize(prog1.get());
110   std::string s2 = Serialize(prog2.get());
111
112   // Don't use EXPECT_EQ because strings are unprintable.
113   EXPECT_FALSE(s1 == s2);  // Unadjusted A and B differ.
114
115   std::unique_ptr<AssemblyProgram> prog5 = MakeProgramA();
116   std::unique_ptr<AssemblyProgram> prog6 = MakeProgramB();
117   Status can_adjust = Adjust(*prog5, prog6.get());
118   EXPECT_EQ(C_OK, can_adjust);
119   std::string s5 = Serialize(prog5.get());
120   std::string s6 = Serialize(prog6.get());
121
122   EXPECT_TRUE(s1 == s5);  // Adjustment did not change A (prog5)
123   EXPECT_TRUE(s5 == s6);  // Adjustment did change B into A
124 }
125
126 TEST_F(AdjustmentMethodTest, All) {
127   Test1();
128 }
129
130 }  // namespace
131
132 }  // namespace courgette