| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/memory_region.h" | 6 #include "vm/memory_region.h" |
| 7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
| 8 | 8 |
| 9 namespace dart { | 9 namespace dart { |
| 10 | 10 |
| 11 static void* NewRegion(uword size) { | 11 static void* NewRegion(uword size) { |
| 12 void* pointer = new uint8_t[size]; | 12 void* pointer = new uint8_t[size]; |
| 13 return pointer; | 13 return pointer; |
| 14 } | 14 } |
| 15 | 15 |
| 16 | 16 |
| 17 static void DeleteRegion(const MemoryRegion& region) { | 17 static void DeleteRegion(const MemoryRegion& region) { |
| 18 delete[] reinterpret_cast<uint8_t*>(region.pointer()); | 18 delete[] reinterpret_cast<uint8_t*>(region.pointer()); |
| 19 } | 19 } |
| 20 | 20 |
| 21 | 21 |
| 22 UNIT_TEST_CASE(NullRegion) { | 22 VM_UNIT_TEST_CASE(NullRegion) { |
| 23 static const uword kSize = 512; | 23 static const uword kSize = 512; |
| 24 MemoryRegion region(NULL, kSize); | 24 MemoryRegion region(NULL, kSize); |
| 25 EXPECT(region.pointer() == NULL); | 25 EXPECT(region.pointer() == NULL); |
| 26 EXPECT_EQ(kSize, region.size()); | 26 EXPECT_EQ(kSize, region.size()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 | 29 |
| 30 UNIT_TEST_CASE(NewRegion) { | 30 VM_UNIT_TEST_CASE(NewRegion) { |
| 31 static const uword kSize = 1024; | 31 static const uword kSize = 1024; |
| 32 MemoryRegion region(NewRegion(kSize), kSize); | 32 MemoryRegion region(NewRegion(kSize), kSize); |
| 33 EXPECT_EQ(kSize, region.size()); | 33 EXPECT_EQ(kSize, region.size()); |
| 34 EXPECT(region.pointer() != NULL); | 34 EXPECT(region.pointer() != NULL); |
| 35 | 35 |
| 36 region.Store<int32_t>(0, 42); | 36 region.Store<int32_t>(0, 42); |
| 37 EXPECT_EQ(42, region.Load<int32_t>(0)); | 37 EXPECT_EQ(42, region.Load<int32_t>(0)); |
| 38 | 38 |
| 39 DeleteRegion(region); | 39 DeleteRegion(region); |
| 40 } | 40 } |
| 41 | 41 |
| 42 | 42 |
| 43 UNIT_TEST_CASE(Subregion) { | 43 VM_UNIT_TEST_CASE(Subregion) { |
| 44 static const uword kSize = 1024; | 44 static const uword kSize = 1024; |
| 45 static const uword kSubOffset = 128; | 45 static const uword kSubOffset = 128; |
| 46 static const uword kSubSize = 512; | 46 static const uword kSubSize = 512; |
| 47 MemoryRegion region(NewRegion(kSize), kSize); | 47 MemoryRegion region(NewRegion(kSize), kSize); |
| 48 MemoryRegion sub_region; | 48 MemoryRegion sub_region; |
| 49 sub_region.Subregion(region, kSubOffset, kSubSize); | 49 sub_region.Subregion(region, kSubOffset, kSubSize); |
| 50 EXPECT_EQ(kSubSize, sub_region.size()); | 50 EXPECT_EQ(kSubSize, sub_region.size()); |
| 51 EXPECT(sub_region.pointer() != NULL); | 51 EXPECT(sub_region.pointer() != NULL); |
| 52 EXPECT(sub_region.start() == region.start() + kSubOffset); | 52 EXPECT(sub_region.start() == region.start() + kSubOffset); |
| 53 | 53 |
| 54 region.Store<int32_t>(0, 42); | 54 region.Store<int32_t>(0, 42); |
| 55 sub_region.Store<int32_t>(0, 100); | 55 sub_region.Store<int32_t>(0, 100); |
| 56 EXPECT_EQ(42, region.Load<int32_t>(0)); | 56 EXPECT_EQ(42, region.Load<int32_t>(0)); |
| 57 EXPECT_EQ(100, region.Load<int32_t>(kSubOffset)); | 57 EXPECT_EQ(100, region.Load<int32_t>(kSubOffset)); |
| 58 | 58 |
| 59 DeleteRegion(region); | 59 DeleteRegion(region); |
| 60 } | 60 } |
| 61 | 61 |
| 62 | 62 |
| 63 UNIT_TEST_CASE(ExtendedRegion) { | 63 VM_UNIT_TEST_CASE(ExtendedRegion) { |
| 64 static const uword kSize = 1024; | 64 static const uword kSize = 1024; |
| 65 static const uword kSubSize = 512; | 65 static const uword kSubSize = 512; |
| 66 static const uword kExtendSize = 512; | 66 static const uword kExtendSize = 512; |
| 67 MemoryRegion region(NewRegion(kSize), kSize); | 67 MemoryRegion region(NewRegion(kSize), kSize); |
| 68 MemoryRegion sub_region; | 68 MemoryRegion sub_region; |
| 69 sub_region.Subregion(region, 0, kSubSize); | 69 sub_region.Subregion(region, 0, kSubSize); |
| 70 MemoryRegion extended_region; | 70 MemoryRegion extended_region; |
| 71 extended_region.Extend(sub_region, kExtendSize); | 71 extended_region.Extend(sub_region, kExtendSize); |
| 72 EXPECT_EQ(kSize, extended_region.size()); | 72 EXPECT_EQ(kSize, extended_region.size()); |
| 73 EXPECT(extended_region.pointer() == region.pointer()); | 73 EXPECT(extended_region.pointer() == region.pointer()); |
| 74 EXPECT(extended_region.pointer() == sub_region.pointer()); | 74 EXPECT(extended_region.pointer() == sub_region.pointer()); |
| 75 | 75 |
| 76 extended_region.Store<int32_t>(0, 42); | 76 extended_region.Store<int32_t>(0, 42); |
| 77 EXPECT_EQ(42, extended_region.Load<int32_t>(0)); | 77 EXPECT_EQ(42, extended_region.Load<int32_t>(0)); |
| 78 | 78 |
| 79 DeleteRegion(region); | 79 DeleteRegion(region); |
| 80 } | 80 } |
| 81 | 81 |
| 82 } // namespace dart | 82 } // namespace dart |
| OLD | NEW |