]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/csharp/test/Apache.Arrow.Tests/ArrowBufferTests.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / test / Apache.Arrow.Tests / ArrowBufferTests.cs
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15
16 using Apache.Arrow.Tests.Fixtures;
17 using System;
18 using System.Runtime.InteropServices;
19 using Xunit;
20
21 namespace Apache.Arrow.Tests
22 {
23 public class ArrowBufferTests
24 {
25 public class Allocate :
26 IClassFixture<DefaultMemoryAllocatorFixture>
27 {
28 private readonly DefaultMemoryAllocatorFixture _memoryPoolFixture;
29
30 public Allocate(DefaultMemoryAllocatorFixture memoryPoolFixture)
31 {
32 _memoryPoolFixture = memoryPoolFixture;
33 }
34
35 /// <summary>
36 /// Ensure Arrow buffers are allocated in multiples of 64 bytes.
37 /// </summary>
38 /// <param name="size">number of bytes to allocate</param>
39 /// <param name="expectedCapacity">expected buffer capacity after allocation</param>
40 [Theory]
41 [InlineData(0, 0)]
42 [InlineData(1, 64)]
43 [InlineData(8, 64)]
44 [InlineData(9, 64)]
45 [InlineData(65, 128)]
46 public void AllocatesWithExpectedPadding(int size, int expectedCapacity)
47 {
48 var builder = new ArrowBuffer.Builder<byte>(size);
49 for (int i = 0; i < size; i++)
50 {
51 builder.Append(0);
52 }
53 var buffer = builder.Build();
54
55 Assert.Equal(expectedCapacity, buffer.Length);
56 }
57
58 /// <summary>
59 /// Ensure allocated buffers are aligned to multiples of 64.
60 /// </summary>
61 [Theory]
62 [InlineData(1)]
63 [InlineData(8)]
64 [InlineData(64)]
65 [InlineData(128)]
66 public unsafe void AllocatesAlignedToMultipleOf64(int size)
67 {
68 var builder = new ArrowBuffer.Builder<byte>(size);
69 for (int i = 0; i < size; i++)
70 {
71 builder.Append(0);
72 }
73 var buffer = builder.Build();
74
75 fixed (byte* ptr = &buffer.Span.GetPinnableReference())
76 {
77 Assert.True(new IntPtr(ptr).ToInt64() % 64 == 0);
78 }
79 }
80
81 /// <summary>
82 /// Ensure padding in arrow buffers is initialized with zeroes.
83 /// </summary>
84 [Fact]
85 public void HasZeroPadding()
86 {
87 var buffer = new ArrowBuffer.Builder<byte>(10).Append(0).Build();
88
89 foreach (var b in buffer.Span)
90 {
91 Assert.Equal(0, b);
92 }
93 }
94
95 }
96
97 [Fact]
98 public void TestExternalMemoryWrappedAsArrowBuffer()
99 {
100 Memory<byte> memory = new byte[sizeof(int) * 3];
101 Span<byte> spanOfBytes = memory.Span;
102 var span = spanOfBytes.CastTo<int>();
103 span[0] = 0;
104 span[1] = 1;
105 span[2] = 2;
106
107 ArrowBuffer buffer = new ArrowBuffer(memory);
108 Assert.Equal(2, buffer.Span.CastTo<int>()[2]);
109
110 span[2] = 10;
111 Assert.Equal(10, buffer.Span.CastTo<int>()[2]);
112 }
113 }
114 }