001package ca.uhn.fhir.test.utilities; 002 003/*- 004 * #%L 005 * HAPI FHIR Test Utilities 006 * %% 007 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import static org.hamcrest.MatcherAssert.assertThat; 024import static org.hamcrest.Matchers.both; 025import static org.hamcrest.Matchers.greaterThanOrEqualTo; 026import static org.hamcrest.Matchers.is; 027import static org.hamcrest.Matchers.lessThanOrEqualTo; 028import static org.junit.jupiter.api.Assertions.assertNotNull; 029 030public class RangeTestHelper { 031 032 public static final double THOUSANDTH = .001d; 033 034 035 public static void checkInRange(double base, double value) { 036 checkInRange(base, THOUSANDTH, value); 037 } 038 039 public static void checkInRange(double theBase, double theRange, double theValue) { 040 double lowerBound = theBase - theRange; 041 double upperBound = theBase + theRange; 042 checkWithinBounds(lowerBound, upperBound, theValue); 043 } 044 045 public static void checkInRange(String theBase, String theValue) { 046 // ease tests 047 if (theBase == null && theValue == null) { 048 return; 049 } 050 051 double value = Double.parseDouble(theValue); 052 double base = Double.parseDouble(theBase); 053 checkInRange(base, THOUSANDTH, value); 054 } 055 056 public static void checkInRange(String theBase, double theRange, String theValue) { 057 // ease tests 058 if (theBase == null && theValue == null) { 059 return; 060 } 061 062 double value = Double.parseDouble(theValue); 063 double base = Double.parseDouble(theBase); 064 checkInRange(base, theRange, value); 065 } 066 067 public static void checkWithinBounds(double theLowerBound, double theUpperBound, double theValue) { 068 assertThat(theValue, is(both(greaterThanOrEqualTo(theLowerBound)).and(lessThanOrEqualTo(theUpperBound)))); 069 } 070 071 public static void checkWithinBounds(String theLowerBound, String theUpperBound, String theValue) { 072 assertNotNull(theLowerBound, "theLowerBound"); 073 assertNotNull(theUpperBound, "theUpperBound"); 074 assertNotNull(theValue, "theValue"); 075 double lowerBound = Double.parseDouble(theLowerBound); 076 double upperBound = Double.parseDouble(theUpperBound); 077 double value = Double.parseDouble(theValue); 078 checkWithinBounds(lowerBound, upperBound, value); 079 } 080 081 082}