Best Assertj code snippet using org.assertj.core.util.NearlyEqualsTest.nearlyEqual
Source:NearlyEqualsTest.java
...20 * Regular large numbers - generally not problematic21 */22 @Test23 public void big() {24 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(1000000.0F, 1000001.0F));25 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(1000001.0F, 1000000.0F));26 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(10000.0F, 10001.0F));27 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(10001.0F, 10000.0F));28 }29 /**30 * Negative large numbers31 */32 @Test33 public void bigNeg() {34 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-1000000.0F), (-1000001.0F)));35 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-1000001.0F), (-1000000.0F)));36 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-10000.0F), (-10001.0F)));37 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-10001.0F), (-10000.0F)));38 }39 /**40 * Numbers around 141 */42 @Test43 public void mid() {44 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(1.0000001F, 1.0000002F));45 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(1.0000002F, 1.0000001F));46 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0002F, 1.0001F));47 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0001F, 1.0002F));48 }49 /**50 * Numbers around -151 */52 @Test53 public void midNeg() {54 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-1.000001F), (-1.000002F)));55 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-1.000002F), (-1.000001F)));56 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.0001F), (-1.0002F)));57 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.0002F), (-1.0001F)));58 }59 /**60 * Numbers between 1 and 061 */62 @Test63 public void small() {64 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(1.000001E-9F, 1.000002E-9F));65 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(1.000002E-9F, 1.000001E-9F));66 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.002E-12F, 1.001E-12F));67 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.001E-12F, 1.002E-12F));68 }69 /**70 * Numbers between -1 and 071 */72 @Test73 public void smallNeg() {74 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-1.000001E-9F), (-1.000002E-9F)));75 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-1.000002E-9F), (-1.000001E-9F)));76 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.002E-12F), (-1.001E-12F)));77 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.001E-12F), (-1.002E-12F)));78 }79 /**80 * Comparisons involving zero81 */82 @Test83 public void zero() {84 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(0.0F, 0.0F));85 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(0.0F, (-0.0F)));86 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-0.0F), (-0.0F)));87 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0E-8F, 0.0F));88 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(0.0F, 1.0E-8F));89 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.0E-8F), 0.0F));90 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(0.0F, (-1.0E-8F)));91 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(0.0F, 1.0E-40F, 0.01F));92 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(1.0E-40F, 0.0F, 0.01F));93 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0E-40F, 0.0F, 1.0E-6F));94 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(0.0F, 1.0E-40F, 1.0E-6F));95 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(0.0F, (-1.0E-40F), 0.1F));96 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-1.0E-40F), 0.0F, 0.1F));97 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.0E-40F), 0.0F, 1.0E-8F));98 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(0.0F, (-1.0E-40F), 1.0E-8F));99 }100 /**101 * Comparisons involving extreme values (overflow potential)102 */103 @Test104 public void extremeMax() {105 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(Float.MAX_VALUE, Float.MAX_VALUE));106 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.MAX_VALUE, (-(Float.MAX_VALUE))));107 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-(Float.MAX_VALUE)), Float.MAX_VALUE));108 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.MAX_VALUE, ((Float.MAX_VALUE) / 2)));109 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.MAX_VALUE, ((-(Float.MAX_VALUE)) / 2)));110 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-(Float.MAX_VALUE)), ((Float.MAX_VALUE) / 2)));111 }112 /**113 * Comparisons involving infinities114 */115 @Test116 public void infinities() {117 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));118 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));119 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY));120 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.POSITIVE_INFINITY, Float.MAX_VALUE));121 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NEGATIVE_INFINITY, (-(Float.MAX_VALUE))));122 }123 /**124 * Comparisons involving NaN values125 */126 @Test127 public void nan() {128 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, Float.NaN));129 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, 0.0F));130 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-0.0F), Float.NaN));131 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, (-0.0F)));132 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(0.0F, Float.NaN));133 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, Float.POSITIVE_INFINITY));134 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.POSITIVE_INFINITY, Float.NaN));135 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, Float.NEGATIVE_INFINITY));136 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NEGATIVE_INFINITY, Float.NaN));137 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, Float.MAX_VALUE));138 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.MAX_VALUE, Float.NaN));139 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, (-(Float.MAX_VALUE))));140 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-(Float.MAX_VALUE)), Float.NaN));141 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, Float.MIN_VALUE));142 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.MIN_VALUE, Float.NaN));143 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.NaN, (-(Float.MIN_VALUE))));144 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-(Float.MIN_VALUE)), Float.NaN));145 }146 /**147 * Comparisons of numbers on opposite sides of 0148 */149 @Test150 public void opposite() {151 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0F, (-1.0F)));152 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.0F), 1.0F));153 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-1.0F), 1.0F));154 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0F, (-1.0F)));155 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((10 * (Float.MIN_VALUE)), (10 * (-(Float.MIN_VALUE)))));156 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((10000 * (Float.MIN_VALUE)), (10000 * (-(Float.MIN_VALUE)))));157 }158 /**159 * The really tricky part - comparisons of numbers very close to zero.160 */161 @Test162 public void ulp() {163 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(Float.MIN_VALUE, Float.MIN_VALUE));164 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(Float.MIN_VALUE, (-(Float.MIN_VALUE))));165 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-(Float.MIN_VALUE)), Float.MIN_VALUE));166 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(Float.MIN_VALUE, 0));167 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(0, Float.MIN_VALUE));168 Assert.assertTrue(NearlyEqualsTest.nearlyEqual((-(Float.MIN_VALUE)), 0));169 Assert.assertTrue(NearlyEqualsTest.nearlyEqual(0, (-(Float.MIN_VALUE))));170 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0E-9F, (-(Float.MIN_VALUE))));171 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(1.0E-9F, Float.MIN_VALUE));172 Assert.assertFalse(NearlyEqualsTest.nearlyEqual(Float.MIN_VALUE, 1.0E-9F));173 Assert.assertFalse(NearlyEqualsTest.nearlyEqual((-(Float.MIN_VALUE)), 1.0E-9F));174 }175}...
nearlyEqual
Using AI Code Generation
1assertThat(1.0).isEqualTo(1.0, within(0.0));2assertThat(1.0).isEqualTo(1.0, within(0.1));3assertThat(1.0).isEqualTo(1.0, within(0.01));4assertThat(1.0).isEqualTo(1.0, within(0.001));5assertThat(1.0).isEqualTo(1.0, within(0.0001));6assertThat(1.0).isEqualTo(1.0, within(0.00001));7assertThat(1.0).isEqualTo(1.0, within(0.000001));8assertThat(1.0).isEqualTo(1.0, within(0.0000001));9assertThat(1.0).isEqualTo(1.0, within(0.00000001));10assertThat(1.0).isEqualTo(1.0, within(0.000000001));11assertThat(1.0).isEqualTo(1.0, within(0.0000000001));12assertThat(1.0).isEqualTo(1.0, within(0.00000000001));13assertThat(1.0).isEqualTo(1.0, within(0.000000000001));14assertThat(1.0).isEqualTo(1.0, within(0.0000000000001));15assertThat(1.0).isEqualTo(1.0, within(0.00000000000001));16assertThat(1.0).isEqualTo(1.0, within(0.000000000000001));17assertThat(1.0).isEqualTo(1.0, within(0.0000000000000001));18assertThat(1.0).isEqualTo(1.0, within(0.00000000000000001));19assertThat(1.0).isEqualTo(1.0, within(0.000000000000000001));20assertThat(1.0).isEqualTo(1.0, within(0.0000000000000000001));21assertThat(1.0).isEqualTo(1.0, within(0.00000000000000000001));22assertThat(1.0).isEqualTo(1.0, within(0.000000000000000000001));
nearlyEqual
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.util.NearlyEquals.nearlyEqual;3public class NearlyEqualsTest {4 public void testNearlyEqual() {5 assertThat(nearlyEqual(0.1, 0.10000000000000001)).isTrue();6 }7}8[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ assertj-core ---9[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ assertj-core ---
nearlyEqual
Using AI Code Generation
1@DisplayName("should return null when no data is available")2void shouldReturnNullWhenNoDataIsAvailable() {3 when(dataService.getData()).thenReturn(null);4 Long result = dataService.getData();5 assertNull(result);6}7public List<SomeObject> getSomeObjects() {8 return someObjectRepository.findAll();9}10public void testGetSomeObjects() {11 List<SomeObject> expected = new ArrayList<>();12 expected.add(new SomeObject(1L, "one"));13 expected.add(new SomeObject(2L, "two"));14 expected.add(new SomeObject(3L, "three"));15 when(someObjectRepository.findAll()).thenReturn(expected);16 List<SomeObject> actual = someObjectService.getSomeObjects();17 assertThat(actual).isEqualTo(expected);18}19 <[SomeObject(id=1, name=one), SomeObject(id=2, name=two), SomeObject(id=3, name=three)]>20 <[SomeObject(id=1, name=one), SomeObject(id=2, name=two), SomeObject(id=3, name=three)]>21public void testGetSomeObjects() {22 List<SomeObject> expected = new ArrayList<>();23 expected.add(new SomeObject(1L, "one"));24 expected.add(new SomeObject(2L, "two"));25 expected.add(new SomeObject(3L, "three"));26 when(someObjectRepository.findAll()).thenReturn(expected);27 List<SomeObject> actual = someObjectService.getSomeObjects();28 assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);29}30 <[SomeObject(id=1, name=one), SomeObject(id=2, name=two), SomeObject(id=3, name=three)]>
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!