|
| 1 | +/** |
| 2 | + * VStar: a statistical analysis tool for variable star data. |
| 3 | + * Copyright (C) 2009 AAVSO (http://www.aavso.org/) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as |
| 7 | + * published by the Free Software Foundation, either version 3 of the |
| 8 | + * License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.aavso.tools.vstar.data; |
| 19 | + |
| 20 | +import junit.framework.TestCase; |
| 21 | + |
| 22 | +import org.quicktheories.WithQuickTheories; |
| 23 | +import org.quicktheories.core.Gen; |
| 24 | + |
| 25 | +/** |
| 26 | + * Property-based tests for the Property class, verifying algebraic contracts |
| 27 | + * of equals, hashCode, and compareTo. |
| 28 | + * |
| 29 | + * These properties are the Java equivalents of the axioms that a formal |
| 30 | + * verification (e.g. in KeY or Lean) would prove unconditionally. |
| 31 | + */ |
| 32 | +public class PropertyPBTTest extends TestCase implements WithQuickTheories { |
| 33 | + |
| 34 | + public PropertyPBTTest(String name) { |
| 35 | + super(name); |
| 36 | + } |
| 37 | + |
| 38 | + // -- Generators for Property instances -- |
| 39 | + |
| 40 | + private Gen<Property> intProperties() { |
| 41 | + return integers().all().map(Property::new); |
| 42 | + } |
| 43 | + |
| 44 | + private Gen<Property> realProperties() { |
| 45 | + return doubles().between(-1e15, 1e15).map(Property::new); |
| 46 | + } |
| 47 | + |
| 48 | + private Gen<Property> boolProperties() { |
| 49 | + return booleans().all().map(Property::new); |
| 50 | + } |
| 51 | + |
| 52 | + private Gen<Property> stringProperties() { |
| 53 | + return strings().basicLatinAlphabet().ofLengthBetween(0, 20) |
| 54 | + .map(Property::new); |
| 55 | + } |
| 56 | + |
| 57 | + // -- equals: reflexivity -- |
| 58 | + |
| 59 | + public void testEqualsReflexiveIntProperty() { |
| 60 | + qt().forAll(intProperties()).check(p -> p.equals(p)); |
| 61 | + } |
| 62 | + |
| 63 | + public void testEqualsReflexiveRealProperty() { |
| 64 | + qt().forAll(realProperties()).check(p -> p.equals(p)); |
| 65 | + } |
| 66 | + |
| 67 | + public void testEqualsReflexiveBoolProperty() { |
| 68 | + qt().forAll(boolProperties()).check(p -> p.equals(p)); |
| 69 | + } |
| 70 | + |
| 71 | + public void testEqualsReflexiveStringProperty() { |
| 72 | + qt().forAll(stringProperties()).check(p -> p.equals(p)); |
| 73 | + } |
| 74 | + |
| 75 | + // -- equals: symmetry (via two identical constructions) -- |
| 76 | + |
| 77 | + public void testEqualsSymmetricIntProperty() { |
| 78 | + qt().forAll(integers().all()).check(v -> { |
| 79 | + Property a = new Property(v); |
| 80 | + Property b = new Property(v); |
| 81 | + return a.equals(b) == b.equals(a); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + public void testEqualsSymmetricRealProperty() { |
| 86 | + qt().forAll(doubles().between(-1e15, 1e15)).check(v -> { |
| 87 | + Property a = new Property(v); |
| 88 | + Property b = new Property(v); |
| 89 | + return a.equals(b) == b.equals(a); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public void testEqualsSymmetricStringProperty() { |
| 94 | + qt().forAll(strings().basicLatinAlphabet().ofLengthBetween(0, 20)) |
| 95 | + .check(v -> { |
| 96 | + Property a = new Property(v); |
| 97 | + Property b = new Property(v); |
| 98 | + return a.equals(b) == b.equals(a); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + // -- hashCode: consistency with equals -- |
| 103 | + |
| 104 | + public void testHashCodeConsistentWithEqualsIntProperty() { |
| 105 | + qt().forAll(integers().all()).check(v -> { |
| 106 | + Property a = new Property(v); |
| 107 | + Property b = new Property(v); |
| 108 | + return !a.equals(b) || a.hashCode() == b.hashCode(); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + public void testHashCodeConsistentWithEqualsRealProperty() { |
| 113 | + qt().forAll(doubles().between(-1e15, 1e15)).check(v -> { |
| 114 | + Property a = new Property(v); |
| 115 | + Property b = new Property(v); |
| 116 | + return !a.equals(b) || a.hashCode() == b.hashCode(); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + public void testHashCodeConsistentWithEqualsStringProperty() { |
| 121 | + qt().forAll(strings().basicLatinAlphabet().ofLengthBetween(0, 20)) |
| 122 | + .check(v -> { |
| 123 | + Property a = new Property(v); |
| 124 | + Property b = new Property(v); |
| 125 | + return !a.equals(b) || a.hashCode() == b.hashCode(); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + // -- compareTo: antisymmetry (sgn(a.compareTo(b)) == -sgn(b.compareTo(a))) -- |
| 130 | + |
| 131 | + public void testCompareToAntisymmetricIntProperty() { |
| 132 | + qt().forAll(integers().all(), integers().all()).check((v1, v2) -> { |
| 133 | + Property a = new Property(v1); |
| 134 | + Property b = new Property(v2); |
| 135 | + return Integer.signum(a.compareTo(b)) == -Integer |
| 136 | + .signum(b.compareTo(a)); |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + public void testCompareToAntisymmetricRealProperty() { |
| 141 | + qt().forAll(doubles().between(-1e15, 1e15), |
| 142 | + doubles().between(-1e15, 1e15)).check((v1, v2) -> { |
| 143 | + Property a = new Property(v1); |
| 144 | + Property b = new Property(v2); |
| 145 | + return Integer.signum(a.compareTo(b)) == -Integer |
| 146 | + .signum(b.compareTo(a)); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + public void testCompareToAntisymmetricStringProperty() { |
| 151 | + qt().forAll(strings().basicLatinAlphabet().ofLengthBetween(0, 20), |
| 152 | + strings().basicLatinAlphabet().ofLengthBetween(0, 20)) |
| 153 | + .check((v1, v2) -> { |
| 154 | + Property a = new Property(v1); |
| 155 | + Property b = new Property(v2); |
| 156 | + return Integer.signum(a.compareTo(b)) == -Integer |
| 157 | + .signum(b.compareTo(a)); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + // -- compareTo: consistency with equals -- |
| 162 | + |
| 163 | + public void testCompareToConsistentWithEqualsIntProperty() { |
| 164 | + qt().forAll(integers().all()).check(v -> { |
| 165 | + Property a = new Property(v); |
| 166 | + Property b = new Property(v); |
| 167 | + return a.equals(b) && a.compareTo(b) == 0; |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + public void testCompareToConsistentWithEqualsRealProperty() { |
| 172 | + qt().forAll(doubles().between(-1e15, 1e15)).check(v -> { |
| 173 | + Property a = new Property(v); |
| 174 | + Property b = new Property(v); |
| 175 | + return a.equals(b) && a.compareTo(b) == 0; |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + public void testCompareToConsistentWithEqualsStringProperty() { |
| 180 | + qt().forAll(strings().basicLatinAlphabet().ofLengthBetween(0, 20)) |
| 181 | + .check(v -> { |
| 182 | + Property a = new Property(v); |
| 183 | + Property b = new Property(v); |
| 184 | + return a.equals(b) && a.compareTo(b) == 0; |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + // -- compareTo: transitivity -- |
| 189 | + |
| 190 | + public void testCompareToTransitiveIntProperty() { |
| 191 | + qt().forAll(integers().all(), integers().all(), integers().all()) |
| 192 | + .check((v1, v2, v3) -> { |
| 193 | + Property a = new Property(v1); |
| 194 | + Property b = new Property(v2); |
| 195 | + Property c = new Property(v3); |
| 196 | + if (a.compareTo(b) <= 0 && b.compareTo(c) <= 0) { |
| 197 | + return a.compareTo(c) <= 0; |
| 198 | + } |
| 199 | + return true; |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + // -- equals: null safety -- |
| 204 | + |
| 205 | + public void testEqualsNullReturnsFalseIntProperty() { |
| 206 | + qt().forAll(intProperties()).check(p -> !p.equals(null)); |
| 207 | + } |
| 208 | + |
| 209 | + // -- NO_VALUE sentinel -- |
| 210 | + |
| 211 | + public void testNoValueEquality() { |
| 212 | + assertTrue(Property.NO_VALUE.equals(new Property())); |
| 213 | + assertEquals(Property.propType.NONE, Property.NO_VALUE.getType()); |
| 214 | + } |
| 215 | +} |
0 commit comments