1 // Copyright (c) 2012 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.
5 #include "courgette/encoded_program.h"
7 #include "courgette/streams.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 TEST(EncodedProgramTest
, Test
) {
12 // Create a simple program with a few addresses and references and
13 // check that the bits produced are as expected.
15 courgette::EncodedProgram
* program
= new courgette::EncodedProgram();
17 uint32 base
= 0x00900000;
18 program
->set_image_base(base
);
20 EXPECT_TRUE(program
->DefineRel32Label(5, 0)); // REL32 index 5 == base + 0
21 EXPECT_TRUE(program
->DefineAbs32Label(7, 4)); // ABS32 index 7 == base + 4
24 EXPECT_TRUE(program
->AddOrigin(0)); // Start at base.
25 EXPECT_TRUE(program
->AddAbs32(7));
26 EXPECT_TRUE(program
->AddRel32(5));
28 // Serialize and deserialize.
30 courgette::SinkStreamSet sinks
;
31 EXPECT_TRUE(program
->WriteTo(&sinks
));
34 courgette::SinkStream sink
;
35 bool can_collect
= sinks
.CopyTo(&sink
);
36 EXPECT_TRUE(can_collect
);
38 const void* buffer
= sink
.Buffer();
39 size_t length
= sink
.Length();
41 courgette::SourceStreamSet sources
;
42 bool can_get_source_streams
= sources
.Init(buffer
, length
);
43 EXPECT_TRUE(can_get_source_streams
);
45 courgette::EncodedProgram
*encoded2
= new courgette::EncodedProgram();
46 bool can_read
= encoded2
->ReadFrom(&sources
);
47 EXPECT_TRUE(can_read
);
49 // Finally, try to assemble.
50 courgette::SinkStream assembled
;
51 bool can_assemble
= encoded2
->AssembleTo(&assembled
);
52 EXPECT_TRUE(can_assemble
);
55 const void* assembled_buffer
= assembled
.Buffer();
56 size_t assembled_length
= assembled
.Length();
58 EXPECT_EQ(8U, assembled_length
);
60 static const uint8 golden
[] = {
61 0x04, 0x00, 0x90, 0x00, // ABS32 to base + 4
62 0xF8, 0xFF, 0xFF, 0xFF // REL32 from next line to base + 2
65 EXPECT_EQ(0, memcmp(assembled_buffer
, golden
, 8));