View Javadoc
1   /*
2    * Copyright (C) 2022-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.containers.test;
5   
6   
7   import static org.junit.jupiter.api.Assertions.assertAll;
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertNotEquals;
11  import static org.junit.jupiter.api.Assertions.assertTrue;
12  
13  import org.junit.jupiter.api.Test;
14  
15  import de.powerstat.validation.containers.NTuple2;
16  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17  
18  
19  /**
20   * NTuple2 tests.
21   */
22  @SuppressFBWarnings({"CE_CLASS_ENVY", "RV_NEGATING_RESULT_OF_COMPARETO", "SPP_USE_ZERO_WITH_COMPARATOR", "EC_NULL_ARG"})
23  final class NTuple2Tests
24   {
25    /**
26     * Default constructor.
27     */
28    /* default */ NTuple2Tests()
29     {
30      super();
31     }
32  
33  
34    /**
35     * Factory test.
36     */
37    @Test
38    /* default */ void testOfInt()
39     {
40      final NTuple2<Integer, Integer> tuple = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
41      assertAll("testGetValue", //$NON-NLS-1$
42        () -> assertEquals(1, tuple.t1Value().intValue(), "t1 not as expected"), //$NON-NLS-1$
43        () -> assertEquals(4711, tuple.t2Value().intValue(), "t2 not as expected") //$NON-NLS-1$
44      );
45     }
46  
47  
48    /**
49     * Test hash code.
50     */
51    @Test
52    /* default */ void testHashCode()
53     {
54      final NTuple2<Integer, Integer> tuple1 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
55      final NTuple2<Integer, Integer> tuple2 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
56      final NTuple2<Integer, Integer> tuple3 = NTuple2.of(Integer.valueOf(2), Integer.valueOf(815));
57      assertAll("testHashCode", //$NON-NLS-1$
58        () -> assertEquals(tuple1.hashCode(), tuple2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
59        () -> assertNotEquals(tuple1.hashCode(), tuple3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
60      );
61     }
62  
63  
64    /**
65     * Test equals.
66     */
67    @Test
68    @SuppressWarnings("java:S5785")
69    /* default */ void testEquals()
70     {
71      final NTuple2<Integer, Integer> tuple1 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
72      final NTuple2<Integer, Integer> tuple2 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
73      final NTuple2<Integer, Integer> tuple3 = NTuple2.of(Integer.valueOf(2), Integer.valueOf(815));
74      final NTuple2<Integer, Integer> tuple4 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
75      assertAll("testEquals", //$NON-NLS-1$
76        () -> assertTrue(tuple1.equals(tuple1), "tuple11 is not equal"), //$NON-NLS-1$
77        () -> assertTrue(tuple1.equals(tuple2), "tuple12 are not equal"), //$NON-NLS-1$
78        () -> assertTrue(tuple2.equals(tuple1), "tuple21 are not equal"), //$NON-NLS-1$
79        () -> assertTrue(tuple2.equals(tuple4), "tuple24 are not equal"), //$NON-NLS-1$
80        () -> assertTrue(tuple1.equals(tuple4), "tuple14 are not equal"), //$NON-NLS-1$
81        () -> assertFalse(tuple1.equals(tuple3), "tuple13 are equal"), //$NON-NLS-1$
82        () -> assertFalse(tuple3.equals(tuple1), "tuple31 are equal"), //$NON-NLS-1$
83        () -> assertFalse(tuple1.equals(null), "tuple10 is equal") //$NON-NLS-1$
84      );
85     }
86  
87  
88    /**
89     * Test toString.
90     */
91    @Test
92    /* default */ void testToString()
93     {
94      final NTuple2<Integer, Integer> tuple1 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
95      assertEquals("NTuple2[object1=1, object2=4711]", tuple1.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
96     }
97  
98  
99    /**
100    * Test compareTo.
101    */
102   @Test
103   @SuppressWarnings("java:S5785")
104   /* default */ void testCompareTo()
105    {
106     final NTuple2<Integer, Integer> tuple1 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
107     final NTuple2<Integer, Integer> tuple2 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
108     final NTuple2<Integer, Integer> tuple3 = NTuple2.of(Integer.valueOf(2), Integer.valueOf(815));
109     final NTuple2<Integer, Integer> tuple4 = NTuple2.of(Integer.valueOf(3), Integer.valueOf(20221126));
110     final NTuple2<Integer, Integer> tuple5 = NTuple2.of(Integer.valueOf(1), Integer.valueOf(4711));
111     assertAll("testCompareTo", //$NON-NLS-1$
112       () -> assertTrue(tuple1.compareTo(tuple2) == -tuple2.compareTo(tuple1), "reflexive1"), //$NON-NLS-1$
113       () -> assertTrue(tuple1.compareTo(tuple3) == -tuple3.compareTo(tuple1), "reflexive2"), //$NON-NLS-1$
114       () -> assertTrue((tuple4.compareTo(tuple3) > 0) && (tuple3.compareTo(tuple1) > 0) && (tuple4.compareTo(tuple1) > 0), "transitive1"), //$NON-NLS-1$
115       () -> assertTrue((tuple1.compareTo(tuple2) == 0) && (Math.abs(tuple1.compareTo(tuple5)) == Math.abs(tuple2.compareTo(tuple5))), "sgn1"), //$NON-NLS-1$
116       () -> assertTrue((tuple1.compareTo(tuple2) == 0) && tuple1.equals(tuple2), "equals") //$NON-NLS-1$
117     );
118    }
119 
120  }