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