Back to home page

OSCL-LXR

 
 

    


0001 /*
0002 * Licensed to the Apache Software Foundation (ASF) under one or more
0003 * contributor license agreements.  See the NOTICE file distributed with
0004 * this work for additional information regarding copyright ownership.
0005 * The ASF licenses this file to You under the Apache License, Version 2.0
0006 * (the "License"); you may not use this file except in compliance with
0007 * the License.  You may obtain a copy of the License at
0008 *
0009 *    http://www.apache.org/licenses/LICENSE-2.0
0010 *
0011 * Unless required by applicable law or agreed to in writing, software
0012 * distributed under the License is distributed on an "AS IS" BASIS,
0013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 * See the License for the specific language governing permissions and
0015 * limitations under the License.
0016 */
0017 
0018 package org.apache.spark.unsafe.types;
0019 
0020 import org.junit.Test;
0021 
0022 import java.time.Duration;
0023 import java.time.Period;
0024 
0025 import static org.junit.Assert.*;
0026 import static org.apache.spark.sql.catalyst.util.DateTimeConstants.*;
0027 
0028 public class CalendarIntervalSuite {
0029 
0030   @Test
0031   public void equalsTest() {
0032     CalendarInterval i1 = new CalendarInterval(3, 2, 123);
0033     CalendarInterval i2 = new CalendarInterval(3, 2,321);
0034     CalendarInterval i3 = new CalendarInterval(3, 4,123);
0035     CalendarInterval i4 = new CalendarInterval(1, 2, 123);
0036     CalendarInterval i5 = new CalendarInterval(1, 4, 321);
0037     CalendarInterval i6 = new CalendarInterval(3, 2, 123);
0038 
0039     assertNotSame(i1, i2);
0040     assertNotSame(i1, i3);
0041     assertNotSame(i1, i4);
0042     assertNotSame(i2, i3);
0043     assertNotSame(i2, i4);
0044     assertNotSame(i3, i4);
0045     assertNotSame(i1, i5);
0046     assertEquals(i1, i6);
0047   }
0048 
0049   @Test
0050   public void toStringTest() {
0051     CalendarInterval i;
0052 
0053     i = new CalendarInterval(0, 0, 0);
0054     assertEquals("0 seconds", i.toString());
0055 
0056     i = new CalendarInterval(34, 0, 0);
0057     assertEquals("2 years 10 months", i.toString());
0058 
0059     i = new CalendarInterval(-34, 0, 0);
0060     assertEquals("-2 years -10 months", i.toString());
0061 
0062     i = new CalendarInterval(0, 31, 0);
0063     assertEquals("31 days", i.toString());
0064 
0065     i = new CalendarInterval(0, -31, 0);
0066     assertEquals("-31 days", i.toString());
0067 
0068     i = new CalendarInterval(0, 0, 3 * MICROS_PER_HOUR + 13 * MICROS_PER_MINUTE + 123);
0069     assertEquals("3 hours 13 minutes 0.000123 seconds", i.toString());
0070 
0071     i = new CalendarInterval(0, 0, -3 * MICROS_PER_HOUR - 13 * MICROS_PER_MINUTE - 123);
0072     assertEquals("-3 hours -13 minutes -0.000123 seconds", i.toString());
0073 
0074     i = new CalendarInterval(34, 31, 3 * MICROS_PER_HOUR + 13 * MICROS_PER_MINUTE + 123);
0075     assertEquals("2 years 10 months 31 days 3 hours 13 minutes 0.000123 seconds",
0076       i.toString());
0077   }
0078 
0079   @Test
0080   public void periodAndDurationTest() {
0081     CalendarInterval interval = new CalendarInterval(120, -40, 123456);
0082     assertEquals(Period.of(0, 120, -40), interval.extractAsPeriod());
0083     assertEquals(Duration.ofNanos(123456000), interval.extractAsDuration());
0084   }
0085 }