|
| 1 | +/* |
| 2 | + * Copyright (C) 2019, Thomas Wolf <[email protected]> |
| 3 | + * and other copyright owners as documented in the project's IP log. |
| 4 | + * |
| 5 | + * This program and the accompanying materials are made available |
| 6 | + * under the terms of the Eclipse Distribution License v1.0 which |
| 7 | + * accompanies this distribution, is reproduced below, and is |
| 8 | + * available at http://www.eclipse.org/org/documents/edl-v10.php |
| 9 | + * |
| 10 | + * All rights reserved. |
| 11 | + * |
| 12 | + * Redistribution and use in source and binary forms, with or |
| 13 | + * without modification, are permitted provided that the following |
| 14 | + * conditions are met: |
| 15 | + * |
| 16 | + * - Redistributions of source code must retain the above copyright |
| 17 | + * notice, this list of conditions and the following disclaimer. |
| 18 | + * |
| 19 | + * - Redistributions in binary form must reproduce the above |
| 20 | + * copyright notice, this list of conditions and the following |
| 21 | + * disclaimer in the documentation and/or other materials provided |
| 22 | + * with the distribution. |
| 23 | + * |
| 24 | + * - Neither the name of the Eclipse Foundation, Inc. nor the |
| 25 | + * names of its contributors may be used to endorse or promote |
| 26 | + * products derived from this software without specific prior |
| 27 | + * written permission. |
| 28 | + * |
| 29 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 30 | + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 31 | + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 32 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 33 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 34 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 35 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 36 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 37 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 38 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 39 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 40 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 41 | + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 | + */ |
| 43 | +package org.eclipse.jgit.treewalk; |
| 44 | + |
| 45 | +import static org.junit.Assert.assertEquals; |
| 46 | +import static org.junit.Assert.assertTrue; |
| 47 | + |
| 48 | +import java.time.Instant; |
| 49 | + |
| 50 | +import org.junit.Test; |
| 51 | + |
| 52 | +public class InstantComparatorTest { |
| 53 | + |
| 54 | + private final InstantComparator cmp = new InstantComparator(); |
| 55 | + |
| 56 | + @Test |
| 57 | + public void compareNow() { |
| 58 | + Instant now = Instant.now(); |
| 59 | + assertEquals(0, cmp.compare(now, now)); |
| 60 | + assertEquals(0, cmp.compare(now, now, true)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void compareSeconds() { |
| 65 | + Instant now = Instant.now(); |
| 66 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond()); |
| 67 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789); |
| 68 | + assertEquals(0, cmp.compare(t, s)); |
| 69 | + assertEquals(0, cmp.compare(t, t)); |
| 70 | + assertEquals(0, cmp.compare(s, t)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void compareSecondsOnly() { |
| 75 | + Instant now = Instant.now(); |
| 76 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 987654321); |
| 77 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789); |
| 78 | + assertEquals(0, cmp.compare(t, s, true)); |
| 79 | + assertEquals(0, cmp.compare(t, t, true)); |
| 80 | + assertEquals(0, cmp.compare(s, t, true)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void compareSecondsUnequal() { |
| 85 | + Instant now = Instant.now(); |
| 86 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond()); |
| 87 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond() - 1L); |
| 88 | + assertTrue(cmp.compare(s, t) < 0); |
| 89 | + assertTrue(cmp.compare(t, s) > 0); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void compareMillisEqual() { |
| 94 | + Instant now = Instant.now(); |
| 95 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000); |
| 96 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789); |
| 97 | + assertEquals(0, cmp.compare(s, t)); |
| 98 | + assertEquals(0, cmp.compare(t, t)); |
| 99 | + assertEquals(0, cmp.compare(t, s)); |
| 100 | + s = Instant.ofEpochSecond(now.getEpochSecond(), 123456000); |
| 101 | + assertEquals(0, cmp.compare(s, t)); |
| 102 | + assertEquals(0, cmp.compare(t, s)); |
| 103 | + s = Instant.ofEpochSecond(now.getEpochSecond(), 123400000); |
| 104 | + assertEquals(0, cmp.compare(s, t)); |
| 105 | + assertEquals(0, cmp.compare(t, s)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void compareMillisUnequal() { |
| 110 | + Instant now = Instant.now(); |
| 111 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000); |
| 112 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 122000000); |
| 113 | + assertTrue(cmp.compare(s, t) < 0); |
| 114 | + assertTrue(cmp.compare(t, s) > 0); |
| 115 | + t = Instant.ofEpochSecond(now.getEpochSecond(), 130000000); |
| 116 | + assertTrue(cmp.compare(s, t) < 0); |
| 117 | + assertTrue(cmp.compare(t, s) > 0); |
| 118 | + t = Instant.ofEpochSecond(now.getEpochSecond(), 200000000); |
| 119 | + assertTrue(cmp.compare(s, t) < 0); |
| 120 | + assertTrue(cmp.compare(t, s) > 0); |
| 121 | + s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123000000); |
| 122 | + assertTrue(cmp.compare(s, t) < 0); |
| 123 | + assertTrue(cmp.compare(t, s) > 0); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void compareMicrosEqual() { |
| 128 | + Instant now = Instant.now(); |
| 129 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000); |
| 130 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789); |
| 131 | + assertEquals(0, cmp.compare(s, t)); |
| 132 | + assertEquals(0, cmp.compare(t, s)); |
| 133 | + s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700); |
| 134 | + assertEquals(0, cmp.compare(s, t)); |
| 135 | + assertEquals(0, cmp.compare(t, s)); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void compareMicrosUnequal() { |
| 140 | + Instant now = Instant.now(); |
| 141 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000); |
| 142 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123455000); |
| 143 | + assertTrue(cmp.compare(s, t) < 0); |
| 144 | + assertTrue(cmp.compare(t, s) > 0); |
| 145 | + t = Instant.ofEpochSecond(now.getEpochSecond(), 123460000); |
| 146 | + assertTrue(cmp.compare(s, t) < 0); |
| 147 | + assertTrue(cmp.compare(t, s) > 0); |
| 148 | + t = Instant.ofEpochSecond(now.getEpochSecond(), 123500000); |
| 149 | + assertTrue(cmp.compare(s, t) < 0); |
| 150 | + assertTrue(cmp.compare(t, s) > 0); |
| 151 | + s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456000); |
| 152 | + assertTrue(cmp.compare(s, t) < 0); |
| 153 | + assertTrue(cmp.compare(t, s) > 0); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void compareNanosEqual() { |
| 158 | + Instant now = Instant.now(); |
| 159 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789); |
| 160 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789); |
| 161 | + assertEquals(0, cmp.compare(s, t)); |
| 162 | + assertEquals(0, cmp.compare(t, s)); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void compareNanosUnequal() { |
| 167 | + Instant now = Instant.now(); |
| 168 | + Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789); |
| 169 | + Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700); |
| 170 | + assertTrue(cmp.compare(s, t) < 0); |
| 171 | + assertTrue(cmp.compare(t, s) > 0); |
| 172 | + t = Instant.ofEpochSecond(now.getEpochSecond(), 123456800); |
| 173 | + assertTrue(cmp.compare(s, t) < 0); |
| 174 | + assertTrue(cmp.compare(t, s) > 0); |
| 175 | + s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456789); |
| 176 | + assertTrue(cmp.compare(s, t) < 0); |
| 177 | + assertTrue(cmp.compare(t, s) > 0); |
| 178 | + s = Instant.ofEpochSecond(now.getEpochSecond(), 123456788); |
| 179 | + assertTrue(cmp.compare(s, t) < 0); |
| 180 | + assertTrue(cmp.compare(t, s) > 0); |
| 181 | + } |
| 182 | +} |
0 commit comments