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.
17 using System.Collections.Generic;
18 using Apache.Arrow.Types;
21 namespace Apache.Arrow.Tests
23 public class Decimal256ArrayTests
27 public class AppendNull
30 public void AppendThenGetGivesNull()
33 var builder = new Decimal256Array.Builder(new Decimal256Type(8,2));
37 builder = builder.AppendNull();
38 builder = builder.AppendNull();
39 builder = builder.AppendNull();
41 var array = builder.Build();
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));
55 public void AppendDecimal(int count)
58 var builder = new Decimal256Array.Builder(new Decimal256Type(14, 10));
61 decimal?[] testData = new decimal?[count];
62 for (int i = 0; i < count; i++)
70 decimal rnd = i * (decimal)Math.Round(new Random().NextDouble(),10);
76 var array = builder.Build();
77 Assert.Equal(count, array.Length);
78 for (int i = 0; i < count; i++)
80 Assert.Equal(testData[i], array.GetValue(i));
85 public void AppendLargeDecimal()
88 var builder = new Decimal256Array.Builder(new Decimal256Type(26, 2));
89 decimal large = 999999999999909999999999.80M;
91 builder.Append(large);
92 builder.Append(-large);
95 var array = builder.Build();
96 Assert.Equal(large, array.GetValue(0));
97 Assert.Equal(-large, array.GetValue(1));
101 public void AppendFractionalDecimal()
104 var builder = new Decimal256Array.Builder(new Decimal256Type(26, 20));
105 decimal fraction = 0.99999999999990999992M;
107 builder.Append(fraction);
108 builder.Append(-fraction);
111 var array = builder.Build();
112 Assert.Equal(fraction, array.GetValue(0));
113 Assert.Equal(-fraction, array.GetValue(1));
117 public void AppendRangeDecimal()
120 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
121 var range = new decimal[] {2.123M, 1.5984M, -0.0000001M, 9878987987987987.1235407M};
124 builder.AppendRange(range);
125 builder.AppendNull();
128 var array = builder.Build();
129 for(int i = 0; i < range.Length; i ++)
131 Assert.Equal(range[i], array.GetValue(i));
134 Assert.Null( array.GetValue(range.Length));
138 public void AppendClearAppendDecimal()
141 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
149 var array = builder.Build();
150 Assert.Equal(10, array.GetValue(0));
154 public void AppendInvalidPrecisionAndScaleDecimal()
157 var builder = new Decimal256Array.Builder(new Decimal256Type(2, 1));
160 Assert.Throws<OverflowException>(() => builder.Append(100));
161 Assert.Throws<OverflowException>(() => builder.Append(0.01M));
162 builder.Append(-9.9M);
164 builder.Append(9.9M);
171 public void SetDecimal()
174 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8))
178 builder.Set(0, 50.123456M);
179 builder.Set(0, 1.01M);
182 var array = builder.Build();
183 Assert.Equal(1.01M, array.GetValue(0));
187 public void SetNull()
190 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8))
194 builder.Set(0, 50.123456M);
198 var array = builder.Build();
199 Assert.Null(array.GetValue(0));
206 public void SetDecimal()
209 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
212 builder.Append(123.45M);
213 builder.Append(678.9M);
217 var array = builder.Build();
218 Assert.Equal(678.9M, array.GetValue(0));
219 Assert.Equal(123.45M, array.GetValue(1));
223 public void SwapNull()
226 var builder = new Decimal256Array.Builder(new Decimal256Type(24, 8));
229 builder.Append(123.456M);
230 builder.AppendNull();
234 var array = builder.Build();
235 Assert.Null(array.GetValue(0));
236 Assert.Equal(123.456M, array.GetValue(1));