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;
21 namespace Apache.Arrow.Tests
23 public class Date32ArrayTests
25 public static IEnumerable<object[]> GetDatesData() =>
26 TestDateAndTimeData.ExampleDates.Select(d => new object[] { d });
28 public static IEnumerable<object[]> GetDateTimesData() =>
29 TestDateAndTimeData.ExampleDateTimes.Select(dt => new object[] { dt });
31 public static IEnumerable<object[]> GetDateTimeOffsetsData() =>
32 TestDateAndTimeData.ExampleDateTimeOffsets.Select(dto => new object[] { dto });
34 public class AppendNull
37 public void AppendThenGetGivesNull()
40 var builder = new Date32Array.Builder();
43 builder = builder.AppendNull();
46 var array = builder.Build();
47 Assert.Equal(1, array.Length);
48 Assert.Null(array.GetDateTime(0));
49 Assert.Null(array.GetDateTimeOffset(0));
50 Assert.Null(array.GetValue(0));
54 public class AppendDateTime
57 [MemberData(nameof(GetDatesData), MemberType = typeof(Date32ArrayTests))]
58 public void AppendDateGivesSameDate(DateTime date)
61 var builder = new Date32Array.Builder();
62 var expectedDateTime = date;
63 var expectedDateTimeOffset =
64 new DateTimeOffset(DateTime.SpecifyKind(date, DateTimeKind.Unspecified), TimeSpan.Zero);
65 int expectedValue = (int)date.Subtract(new DateTime(1970, 1, 1)).TotalDays;
68 builder = builder.Append(date);
71 var array = builder.Build();
72 Assert.Equal(1, array.Length);
73 Assert.Equal(expectedDateTime, array.GetDateTime(0));
74 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
75 Assert.Equal(expectedValue, array.GetValue(0));
79 [MemberData(nameof(GetDateTimesData), MemberType = typeof(Date32ArrayTests))]
80 public void AppendWithTimeGivesSameWithTimeIgnored(DateTime dateTime)
83 var builder = new Date32Array.Builder();
84 var expectedDateTime = dateTime.Date;
85 var expectedDateTimeOffset =
86 new DateTimeOffset(DateTime.SpecifyKind(dateTime.Date, DateTimeKind.Unspecified), TimeSpan.Zero);
87 int expectedValue = (int)dateTime.Date.Subtract(new DateTime(1970, 1, 1)).TotalDays;
90 builder = builder.Append(dateTime);
93 var array = builder.Build();
94 Assert.Equal(1, array.Length);
95 Assert.Equal(expectedDateTime, array.GetDateTime(0));
96 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
97 Assert.Equal(expectedValue, array.GetValue(0));
101 public class AppendDateTimeOffset
104 [MemberData(nameof(GetDateTimeOffsetsData), MemberType = typeof(Date32ArrayTests))]
105 public void AppendGivesUtcDate(DateTimeOffset dateTimeOffset)
108 var builder = new Date32Array.Builder();
109 var expectedDateTime = dateTimeOffset.UtcDateTime.Date;
110 var expectedDateTimeOffset = new DateTimeOffset(dateTimeOffset.UtcDateTime.Date, TimeSpan.Zero);
111 int expectedValue = (int)dateTimeOffset.UtcDateTime.Date.Subtract(new DateTime(1970, 1, 1)).TotalDays;
114 builder = builder.Append(dateTimeOffset);
117 var array = builder.Build();
118 Assert.Equal(1, array.Length);
119 Assert.Equal(expectedDateTime, array.GetDateTime(0));
120 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
121 Assert.Equal(expectedValue, array.GetValue(0));