1 : /*
2 : * Copyright 2012 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can
4 : * be found in the LICENSE file.
5 : * Copyright 2012, Google Inc.
6 : */
7 :
8 : /*
9 : * A simple test for AddressSet.
10 : */
11 :
12 : #include <vector>
13 : #include <string>
14 : #include <sstream>
15 : #include <stdio.h>
16 : #include <stdlib.h>
17 :
18 : #include "gtest/gtest.h"
19 : #include "native_client/src/include/nacl_macros.h"
20 : #include "native_client/src/include/portability.h"
21 : #include "native_client/src/trusted/validator_mips/address_set.h"
22 :
23 : using nacl_mips_val::AddressSet;
24 :
25 :
26 2 : class AddressTests : public ::testing::Test {
27 : };
28 :
29 5 : TEST_F(AddressTests, TestMutation) {
30 1 : uint32_t base = 0x1234;
31 1 : uint32_t size = 0x1000;
32 1 : AddressSet as(base, size);
33 :
34 1 : as.Add(0x1200); // Becomes a no-op.
35 1 : as.Add(base + (31 * 4)); // Added.
36 1 : as.Add(0x1240); // Added.
37 1 : as.Add(0x1230); // No-op.
38 1 : as.Add(base + size); // No-op.
39 1 : as.Add(0x1235); // Added as 1234.
40 1 : as.Add(0x1238); // Added.
41 1 : as.Add(0x2000); // Added.
42 1 : as.Add(base + size + 100); // No-op.
43 1 : as.Add(0x1400); // Added.
44 :
45 : // Successful additions in ascending order:
46 1 : uint32_t expected[] = { 0x1234, 0x1238, 0x1240, base+(31*4), 0x1400, 0x2000 };
47 7 : for (uint32_t i = 0; i < NACL_ARRAY_SIZE(expected); i++) {
48 6 : EXPECT_TRUE(as.Contains(expected[i])) << "Set should contain "
49 0 : << std::hex << expected[i]
50 6 : << ", does not.";
51 : }
52 :
53 1 : uint32_t x = 0;
54 7 : for (AddressSet::Iterator it = as.Begin(); !it.Equals(as.End());
55 : it.Next(), ++x) {
56 6 : EXPECT_EQ(it.GetAddress(), expected[x]) << "At " << x
57 0 : << "\n Expected: " << std::hex
58 0 : << expected[x]
59 0 : << "\n Actual: " << std::hex
60 6 : << it.GetAddress();
61 : }
62 1 : EXPECT_EQ(x, NACL_ARRAY_SIZE(expected)) << "Expected iterator to step"
63 0 : << NACL_ARRAY_SIZE(expected)
64 1 : << " times \n" << "Actual: " << x;
65 :
66 : // Unsuccessful additions:
67 1 : uint32_t unexpected[] = { 0x1200, 0x1230, base+size, base+size+100 };
68 5 : for (uint32_t i = 0; i < NACL_ARRAY_SIZE(unexpected); i++) {
69 4 : EXPECT_FALSE(as.Contains(unexpected[i])) << "Set should not contain "
70 4 : << std::hex << unexpected[i];
71 1 : }
72 1 : }
73 :
74 1 : int main(int argc, char *argv[]) {
75 1 : testing::InitGoogleTest(&argc, argv);
76 1 : return RUN_ALL_TESTS();
77 3 : }
|