]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/csharp/test/Apache.Arrow.Tests/Decimal256ArrayTests.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / test / Apache.Arrow.Tests / Decimal256ArrayTests.cs
CommitLineData
1d09f67e
TL
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
16using System;
17using System.Collections.Generic;
18using Apache.Arrow.Types;
19using Xunit;
20
21namespace Apache.Arrow.Tests
22{
23 public class Decimal256ArrayTests
24 {
25 public class Builder
26 {
27 public class AppendNull
28 {
29 [Fact]
30 public void AppendThenGetGivesNull()
31 {
32 // Arrange
33 var builder = new Decimal256Array.Builder(new Decimal256Type(8,2));
34
35 // Act
36
37 builder = builder.AppendNull();
38 builder = builder.AppendNull();
39 builder = builder.AppendNull();
40 // Assert
41 var array = builder.Build();
42
43 Assert.Equal(3, array.Length);
44 Assert.Equal(array.Data.Buffers[1].Length, array.ByteWidth * 3);
45 Assert.Null(array.GetValue(0));
46 Assert.Null(array.GetValue(1));
47 Assert.Null(array.GetValue(2));
48 }
49 }
50
51 public class Append
52 {
53 [Theory]
54 [InlineData(200)]
55 public void AppendDecimal(int count)
56 {
57 // Arrange
58 var builder = new Decimal256Array.Builder(new Decimal256Type(14, 10));
59
60 // Act
61 decimal?[] testData = new decimal?[count];
62 for (int i = 0; i < count; i++)
63 {
64 if (i == count - 2)
65 {
66 builder.AppendNull();
67 testData[i] = null;
68 continue;
69 }
70 decimal rnd = i * (decimal)Math.Round(new Random().NextDouble(),10);
71 testData[i] = rnd;
72 builder.Append(rnd);
73 }
74
75 // Assert
76 var array = builder.Build();
77 Assert.Equal(count, array.Length);
78 for (int i = 0; i < count; i++)
79 {
80 Assert.Equal(testData[i], array.GetValue(i));
81 }
82 }
83
84 [Fact]
85 public void AppendLargeDecimal()
86 {
87 // Arrange
88 var builder = new Decimal256Array.Builder(new Decimal256Type(26, 2));
89 decimal large = 999999999999909999999999.80M;
90 // Act
91 builder.Append(large);
92 builder.Append(-large);
93
94 // Assert
95 var array = builder.Build();
96 Assert.Equal(large, array.GetValue(0));
97 Assert.Equal(-large, array.GetValue(1));
98 }
99
100 [Fact]
101 public void AppendFractionalDecimal()
102 {
103 // Arrange
104 var builder = new Decimal256Array.Builder(new Decimal256Type(26, 20));
105 decimal fraction = 0.99999999999990999992M;
106 // Act
107 builder.Append(fraction);
108 builder.Append(-fraction);
109
110 // Assert
111 var array = builder.Build();
112 Assert.Equal(fraction, array.GetValue(0));
113 Assert.Equal(-fraction, array.GetValue(1));
114 }
115
116 [Fact]
117 public void AppendRangeDecimal()
118 {
119 // Arrange
120 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
121 var range = new decimal[] {2.123M, 1.5984M, -0.0000001M, 9878987987987987.1235407M};
122
123 // Act
124 builder.AppendRange(range);
125 builder.AppendNull();
126
127 // Assert
128 var array = builder.Build();
129 for(int i = 0; i < range.Length; i ++)
130 {
131 Assert.Equal(range[i], array.GetValue(i));
132 }
133
134 Assert.Null( array.GetValue(range.Length));
135 }
136
137 [Fact]
138 public void AppendClearAppendDecimal()
139 {
140 // Arrange
141 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
142
143 // Act
144 builder.Append(1);
145 builder.Clear();
146 builder.Append(10);
147
148 // Assert
149 var array = builder.Build();
150 Assert.Equal(10, array.GetValue(0));
151 }
152
153 [Fact]
154 public void AppendInvalidPrecisionAndScaleDecimal()
155 {
156 // Arrange
157 var builder = new Decimal256Array.Builder(new Decimal256Type(2, 1));
158
159 // Assert
160 Assert.Throws<OverflowException>(() => builder.Append(100));
161 Assert.Throws<OverflowException>(() => builder.Append(0.01M));
162 builder.Append(-9.9M);
163 builder.Append(0);
164 builder.Append(9.9M);
165 }
166 }
167
168 public class Set
169 {
170 [Fact]
171 public void SetDecimal()
172 {
173 // Arrange
174 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8))
175 .Resize(1);
176
177 // Act
178 builder.Set(0, 50.123456M);
179 builder.Set(0, 1.01M);
180
181 // Assert
182 var array = builder.Build();
183 Assert.Equal(1.01M, array.GetValue(0));
184 }
185
186 [Fact]
187 public void SetNull()
188 {
189 // Arrange
190 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8))
191 .Resize(1);
192
193 // Act
194 builder.Set(0, 50.123456M);
195 builder.SetNull(0);
196
197 // Assert
198 var array = builder.Build();
199 Assert.Null(array.GetValue(0));
200 }
201 }
202
203 public class Swap
204 {
205 [Fact]
206 public void SetDecimal()
207 {
208 // Arrange
209 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
210
211 // Act
212 builder.Append(123.45M);
213 builder.Append(678.9M);
214 builder.Swap(0, 1);
215
216 // Assert
217 var array = builder.Build();
218 Assert.Equal(678.9M, array.GetValue(0));
219 Assert.Equal(123.45M, array.GetValue(1));
220 }
221
222 [Fact]
223 public void SwapNull()
224 {
225 // Arrange
226 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
227
228 // Act
229 builder.Append(123.456M);
230 builder.AppendNull();
231 builder.Swap(0, 1);
232
233 // Assert
234 var array = builder.Build();
235 Assert.Null(array.GetValue(0));
236 Assert.Equal(123.456M, array.GetValue(1));
237 }
238 }
239 }
240 }
241}