| 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 "vm/memory_region.h" |
| 5 #include "platform/assert.h" | 6 #include "platform/assert.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 | |
| 17 static void DeleteRegion(const MemoryRegion& region) { | 16 static void DeleteRegion(const MemoryRegion& region) { |
| 18 delete[] reinterpret_cast<uint8_t*>(region.pointer()); | 17 delete[] reinterpret_cast<uint8_t*>(region.pointer()); |
| 19 } | 18 } |
| 20 | 19 |
| 21 | |
| 22 VM_UNIT_TEST_CASE(NullRegion) { | 20 VM_UNIT_TEST_CASE(NullRegion) { |
| 23 static const uword kSize = 512; | 21 static const uword kSize = 512; |
| 24 MemoryRegion region(NULL, kSize); | 22 MemoryRegion region(NULL, kSize); |
| 25 EXPECT(region.pointer() == NULL); | 23 EXPECT(region.pointer() == NULL); |
| 26 EXPECT_EQ(kSize, region.size()); | 24 EXPECT_EQ(kSize, region.size()); |
| 27 } | 25 } |
| 28 | 26 |
| 29 | |
| 30 VM_UNIT_TEST_CASE(NewRegion) { | 27 VM_UNIT_TEST_CASE(NewRegion) { |
| 31 static const uword kSize = 1024; | 28 static const uword kSize = 1024; |
| 32 MemoryRegion region(NewRegion(kSize), kSize); | 29 MemoryRegion region(NewRegion(kSize), kSize); |
| 33 EXPECT_EQ(kSize, region.size()); | 30 EXPECT_EQ(kSize, region.size()); |
| 34 EXPECT(region.pointer() != NULL); | 31 EXPECT(region.pointer() != NULL); |
| 35 | 32 |
| 36 region.Store<int32_t>(0, 42); | 33 region.Store<int32_t>(0, 42); |
| 37 EXPECT_EQ(42, region.Load<int32_t>(0)); | 34 EXPECT_EQ(42, region.Load<int32_t>(0)); |
| 38 | 35 |
| 39 DeleteRegion(region); | 36 DeleteRegion(region); |
| 40 } | 37 } |
| 41 | 38 |
| 42 | |
| 43 VM_UNIT_TEST_CASE(Subregion) { | 39 VM_UNIT_TEST_CASE(Subregion) { |
| 44 static const uword kSize = 1024; | 40 static const uword kSize = 1024; |
| 45 static const uword kSubOffset = 128; | 41 static const uword kSubOffset = 128; |
| 46 static const uword kSubSize = 512; | 42 static const uword kSubSize = 512; |
| 47 MemoryRegion region(NewRegion(kSize), kSize); | 43 MemoryRegion region(NewRegion(kSize), kSize); |
| 48 MemoryRegion sub_region; | 44 MemoryRegion sub_region; |
| 49 sub_region.Subregion(region, kSubOffset, kSubSize); | 45 sub_region.Subregion(region, kSubOffset, kSubSize); |
| 50 EXPECT_EQ(kSubSize, sub_region.size()); | 46 EXPECT_EQ(kSubSize, sub_region.size()); |
| 51 EXPECT(sub_region.pointer() != NULL); | 47 EXPECT(sub_region.pointer() != NULL); |
| 52 EXPECT(sub_region.start() == region.start() + kSubOffset); | 48 EXPECT(sub_region.start() == region.start() + kSubOffset); |
| 53 | 49 |
| 54 region.Store<int32_t>(0, 42); | 50 region.Store<int32_t>(0, 42); |
| 55 sub_region.Store<int32_t>(0, 100); | 51 sub_region.Store<int32_t>(0, 100); |
| 56 EXPECT_EQ(42, region.Load<int32_t>(0)); | 52 EXPECT_EQ(42, region.Load<int32_t>(0)); |
| 57 EXPECT_EQ(100, region.Load<int32_t>(kSubOffset)); | 53 EXPECT_EQ(100, region.Load<int32_t>(kSubOffset)); |
| 58 | 54 |
| 59 DeleteRegion(region); | 55 DeleteRegion(region); |
| 60 } | 56 } |
| 61 | 57 |
| 62 | |
| 63 VM_UNIT_TEST_CASE(ExtendedRegion) { | 58 VM_UNIT_TEST_CASE(ExtendedRegion) { |
| 64 static const uword kSize = 1024; | 59 static const uword kSize = 1024; |
| 65 static const uword kSubSize = 512; | 60 static const uword kSubSize = 512; |
| 66 static const uword kExtendSize = 512; | 61 static const uword kExtendSize = 512; |
| 67 MemoryRegion region(NewRegion(kSize), kSize); | 62 MemoryRegion region(NewRegion(kSize), kSize); |
| 68 MemoryRegion sub_region; | 63 MemoryRegion sub_region; |
| 69 sub_region.Subregion(region, 0, kSubSize); | 64 sub_region.Subregion(region, 0, kSubSize); |
| 70 MemoryRegion extended_region; | 65 MemoryRegion extended_region; |
| 71 extended_region.Extend(sub_region, kExtendSize); | 66 extended_region.Extend(sub_region, kExtendSize); |
| 72 EXPECT_EQ(kSize, extended_region.size()); | 67 EXPECT_EQ(kSize, extended_region.size()); |
| 73 EXPECT(extended_region.pointer() == region.pointer()); | 68 EXPECT(extended_region.pointer() == region.pointer()); |
| 74 EXPECT(extended_region.pointer() == sub_region.pointer()); | 69 EXPECT(extended_region.pointer() == sub_region.pointer()); |
| 75 | 70 |
| 76 extended_region.Store<int32_t>(0, 42); | 71 extended_region.Store<int32_t>(0, 42); |
| 77 EXPECT_EQ(42, extended_region.Load<int32_t>(0)); | 72 EXPECT_EQ(42, extended_region.Load<int32_t>(0)); |
| 78 | 73 |
| 79 DeleteRegion(region); | 74 DeleteRegion(region); |
| 80 } | 75 } |
| 81 | 76 |
| 82 } // namespace dart | 77 } // namespace dart |
| OLD | NEW |