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/path_service.h"
8 #include "base/file_util.h"
9 #include "base/string_util.h"
11 #include "courgette/assembly_program.h"
12 #include "courgette/courgette.h"
13 #include "courgette/streams.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 class AdjustmentMethodTest
: public testing::Test
{
28 // Returns one of two similar a simple programs. They differ only in the
29 // label assignment, so that it is possible to make them look identical.
30 courgette::AssemblyProgram
* MakeProgram(int kind
) const {
31 courgette::AssemblyProgram
* prog
= new courgette::AssemblyProgram();
32 prog
->set_image_base(0x00400000);
34 courgette::Label
* labelA
= prog
->FindOrMakeAbs32Label(0x00410000);
35 courgette::Label
* labelB
= prog
->FindOrMakeAbs32Label(0x00410004);
37 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
38 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
39 EXPECT_TRUE(prog
->EmitAbs32(labelB
));
40 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
41 EXPECT_TRUE(prog
->EmitAbs32(labelA
));
42 EXPECT_TRUE(prog
->EmitAbs32(labelB
));
51 prog
->AssignRemainingIndexes();
56 courgette::AssemblyProgram
* MakeProgramA() const { return MakeProgram(0); }
57 courgette::AssemblyProgram
* MakeProgramB() const { return MakeProgram(1); }
59 // Returns a string that is the serialized version of |program|.
61 std::string
Serialize(courgette::AssemblyProgram
*program
) const {
62 courgette::EncodedProgram
* encoded
= NULL
;
64 const courgette::Status encode_status
= Encode(program
, &encoded
);
65 EXPECT_EQ(courgette::C_OK
, encode_status
);
67 DeleteAssemblyProgram(program
);
69 courgette::SinkStreamSet sinks
;
70 const courgette::Status write_status
= WriteEncodedProgram(encoded
, &sinks
);
71 EXPECT_EQ(courgette::C_OK
, write_status
);
73 DeleteEncodedProgram(encoded
);
75 courgette::SinkStream sink
;
76 bool can_collect
= sinks
.CopyTo(&sink
);
77 EXPECT_TRUE(can_collect
);
79 return std::string(reinterpret_cast<const char *>(sink
.Buffer()),
85 void AdjustmentMethodTest::Test1() const {
86 courgette::AssemblyProgram
* prog1
= MakeProgramA();
87 courgette::AssemblyProgram
* prog2
= MakeProgramB();
88 std::string s1
= Serialize(prog1
);
89 std::string s2
= Serialize(prog2
);
91 // Don't use EXPECT_EQ because strings are unprintable.
92 EXPECT_FALSE(s1
== s2
); // Unadjusted A and B differ.
94 courgette::AssemblyProgram
* prog5
= MakeProgramA();
95 courgette::AssemblyProgram
* prog6
= MakeProgramB();
96 courgette::Status can_adjust
= Adjust(*prog5
, prog6
);
97 EXPECT_EQ(courgette::C_OK
, can_adjust
);
98 std::string s5
= Serialize(prog5
);
99 std::string s6
= Serialize(prog6
);
101 EXPECT_TRUE(s1
== s5
); // Adjustment did not change A (prog5)
102 EXPECT_TRUE(s5
== s6
); // Adjustment did change B into A
106 TEST_F(AdjustmentMethodTest
, All
) {