1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License. You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 using Apache.Arrow.Tests.Fixtures;
18 using System.Runtime.InteropServices;
21 namespace Apache.Arrow.Tests
23 public class ArrowBufferTests
25 public class Allocate :
26 IClassFixture<DefaultMemoryAllocatorFixture>
28 private readonly DefaultMemoryAllocatorFixture _memoryPoolFixture;
30 public Allocate(DefaultMemoryAllocatorFixture memoryPoolFixture)
32 _memoryPoolFixture = memoryPoolFixture;
36 /// Ensure Arrow buffers are allocated in multiples of 64 bytes.
38 /// <param name="size">number of bytes to allocate</param>
39 /// <param name="expectedCapacity">expected buffer capacity after allocation</param>
46 public void AllocatesWithExpectedPadding(int size, int expectedCapacity)
48 var builder = new ArrowBuffer.Builder<byte>(size);
49 for (int i = 0; i < size; i++)
53 var buffer = builder.Build();
55 Assert.Equal(expectedCapacity, buffer.Length);
59 /// Ensure allocated buffers are aligned to multiples of 64.
66 public unsafe void AllocatesAlignedToMultipleOf64(int size)
68 var builder = new ArrowBuffer.Builder<byte>(size);
69 for (int i = 0; i < size; i++)
73 var buffer = builder.Build();
75 fixed (byte* ptr = &buffer.Span.GetPinnableReference())
77 Assert.True(new IntPtr(ptr).ToInt64() % 64 == 0);
82 /// Ensure padding in arrow buffers is initialized with zeroes.
85 public void HasZeroPadding()
87 var buffer = new ArrowBuffer.Builder<byte>(10).Append(0).Build();
89 foreach (var b in buffer.Span)
98 public void TestExternalMemoryWrappedAsArrowBuffer()
100 Memory<byte> memory = new byte[sizeof(int) * 3];
101 Span<byte> spanOfBytes = memory.Span;
102 var span = spanOfBytes.CastTo<int>();
107 ArrowBuffer buffer = new ArrowBuffer(memory);
108 Assert.Equal(2, buffer.Span.CastTo<int>()[2]);
111 Assert.Equal(10, buffer.Span.CastTo<int>()[2]);