#include <stddef.h>
#include <stdint.h>
#include <memory>
#include "build/rust/tests/test_rust_shared_library/src/lib.rs.h"
#include "partition_alloc/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"
#if PA_BUILDFLAG(HAS_64_BIT_POINTERS)
#include "partition_alloc/partition_address_space.h"
#else
#include "partition_alloc/address_pool_manager_bitmap.h"
#endif
TEST(RustSharedTest, CppCallingIntoRust_BasicFFI) {
EXPECT_EQ(7, add_two_ints_via_rust(3, 4));
}
TEST(RustSharedTest, RustComponentUsesPartitionAlloc) {
auto cpp_allocated_int = std::make_unique<int>();
SomeStruct* rust_allocated_ptr = allocate_via_rust().into_raw();
EXPECT_EQ(partition_alloc::IsManagedByPartitionAlloc(
reinterpret_cast<uintptr_t>(rust_allocated_ptr)),
partition_alloc::IsManagedByPartitionAlloc(
reinterpret_cast<uintptr_t>(cpp_allocated_int.get())));
rust::Box<SomeStruct>::from_raw(rust_allocated_ptr);
}
TEST(RustSharedTest, AllocAligned) {
alloc_aligned();
}
TEST(RustSharedTest, DISABLED_RustLargeAllocationFailure) {
EXPECT_TRUE(allocate_huge_via_rust(100u, 1u));
#if PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
size_t max_size = partition_alloc::internal::MaxDirectMapped();
EXPECT_FALSE(allocate_huge_via_rust(max_size + 1u, 4u));
size_t big_alignment = alignof(std::max_align_t) * 2u;
EXPECT_FALSE(allocate_huge_via_rust(max_size + 1u, big_alignment));
size_t max_alignment = partition_alloc::internal::kMaxSupportedAlignment;
EXPECT_FALSE(allocate_huge_via_rust(100u, max_alignment * 2u));
#endif
}