1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/strings/string_util.h"
9 #include "courgette/assembly_program.h"
10 #include "courgette/courgette.h"
11 #include "courgette/streams.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 class AdjustmentMethodTest
: public testing::Test
{
26 // Returns one of two similar a simple programs. They differ only in the
27 // label assignment, so that it is possible to make them look identical.
28 courgette::AssemblyProgram
* MakeProgram(int kind
) const {
29 courgette::AssemblyProgram
* prog
= new courgette::AssemblyProgram();
30 prog
->set_image_base(0x00400000);
32 courgette::Label
* labelA
= prog
->FindOrMakeAbs32Label(0x00410000);
33 courgette::Label
* labelB
= prog
->FindOrMakeAbs32Label(0x00410004);
35 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
36 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
37 EXPECT_TRUE(prog
->EmitAbs32(labelB
));
38 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
39 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
40 EXPECT_TRUE(prog
->EmitAbs32(labelB
));
49 prog
->AssignRemainingIndexes();
54 courgette::AssemblyProgram
* MakeProgramA() const { return MakeProgram(0); }
55 courgette::AssemblyProgram
* MakeProgramB() const { return MakeProgram(1); }
57 // Returns a string that is the serialized version of |program|.
59 std::string
Serialize(courgette::AssemblyProgram
*program
) const {
60 courgette::EncodedProgram
* encoded
= NULL
;
62 const courgette::Status encode_status
= Encode(program
, &encoded
);
63 EXPECT_EQ(courgette::C_OK
, encode_status
);
65 DeleteAssemblyProgram(program
);
67 courgette::SinkStreamSet sinks
;
68 const courgette::Status write_status
= WriteEncodedProgram(encoded
, &sinks
);
69 EXPECT_EQ(courgette::C_OK
, write_status
);
71 DeleteEncodedProgram(encoded
);
73 courgette::SinkStream sink
;
74 bool can_collect
= sinks
.CopyTo(&sink
);
75 EXPECT_TRUE(can_collect
);
77 return std::string(reinterpret_cast<const char *>(sink
.Buffer()),
83 void AdjustmentMethodTest::Test1() const {
84 courgette::AssemblyProgram
* prog1
= MakeProgramA();
85 courgette::AssemblyProgram
* prog2
= MakeProgramB();
86 std::string s1
= Serialize(prog1
);
87 std::string s2
= Serialize(prog2
);
89 // Don't use EXPECT_EQ because strings are unprintable.
90 EXPECT_FALSE(s1
== s2
); // Unadjusted A and B differ.
92 courgette::AssemblyProgram
* prog5
= MakeProgramA();
93 courgette::AssemblyProgram
* prog6
= MakeProgramB();
94 courgette::Status can_adjust
= Adjust(*prog5
, prog6
);
95 EXPECT_EQ(courgette::C_OK
, can_adjust
);
96 std::string s5
= Serialize(prog5
);
97 std::string s6
= Serialize(prog6
);
99 EXPECT_TRUE(s1
== s5
); // Adjustment did not change A (prog5)
100 EXPECT_TRUE(s5
== s6
); // Adjustment did change B into A
104 TEST_F(AdjustmentMethodTest
, All
) {