dart-sdk/runtime/vm/memory_region_test.cc
Ryan Macnak 2bfecc160b [vm] Update to constexpr in runtime/vm.
TEST=build
Change-Id: I2dd8ae69764af27f480a19995b491e98f52476ae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293902
Reviewed-by: Liam Appelbe <liama@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
2023-04-12 22:18:54 +00:00

77 lines
2.2 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/memory_region.h"
#include "platform/assert.h"
#include "vm/unit_test.h"
namespace dart {
static void* NewRegion(uword size) {
void* pointer = new uint8_t[size];
return pointer;
}
static void DeleteRegion(const MemoryRegion& region) {
delete[] reinterpret_cast<uint8_t*>(region.pointer());
}
VM_UNIT_TEST_CASE(NullRegion) {
const uword kSize = 512;
MemoryRegion region(nullptr, kSize);
EXPECT(region.pointer() == nullptr);
EXPECT_EQ(kSize, region.size());
}
VM_UNIT_TEST_CASE(NewRegion) {
const uword kSize = 1024;
MemoryRegion region(NewRegion(kSize), kSize);
EXPECT_EQ(kSize, region.size());
EXPECT(region.pointer() != nullptr);
region.Store<int32_t>(0, 42);
EXPECT_EQ(42, region.Load<int32_t>(0));
DeleteRegion(region);
}
VM_UNIT_TEST_CASE(Subregion) {
const uword kSize = 1024;
const uword kSubOffset = 128;
const uword kSubSize = 512;
MemoryRegion region(NewRegion(kSize), kSize);
MemoryRegion sub_region;
sub_region.Subregion(region, kSubOffset, kSubSize);
EXPECT_EQ(kSubSize, sub_region.size());
EXPECT(sub_region.pointer() != nullptr);
EXPECT(sub_region.start() == region.start() + kSubOffset);
region.Store<int32_t>(0, 42);
sub_region.Store<int32_t>(0, 100);
EXPECT_EQ(42, region.Load<int32_t>(0));
EXPECT_EQ(100, region.Load<int32_t>(kSubOffset));
DeleteRegion(region);
}
VM_UNIT_TEST_CASE(ExtendedRegion) {
const uword kSize = 1024;
const uword kSubSize = 512;
const uword kExtendSize = 512;
MemoryRegion region(NewRegion(kSize), kSize);
MemoryRegion sub_region;
sub_region.Subregion(region, 0, kSubSize);
MemoryRegion extended_region;
extended_region.Extend(sub_region, kExtendSize);
EXPECT_EQ(kSize, extended_region.size());
EXPECT(extended_region.pointer() == region.pointer());
EXPECT(extended_region.pointer() == sub_region.pointer());
extended_region.Store<int32_t>(0, 42);
EXPECT_EQ(42, extended_region.Load<int32_t>(0));
DeleteRegion(region);
}
} // namespace dart