From e50af0f011ea727603ba475dae3677735ba1061e Mon Sep 17 00:00:00 2001 From: Dzina Dybouskaya Date: Thu, 5 Jan 2023 18:42:18 +0300 Subject: [PATCH] NAVAND-979: use distance-dependent default rounding increments --- changelog/unreleased/bugfixes/dd2.md | 1 + libnavigation-base/api/current.txt | 3 +- .../formatter/DistanceFormatterOptions.kt | 2 +- .../navigation/base/formatter/Rounding.kt | 7 + .../core/formatter/MapboxDistanceUtil.kt | 58 ++- .../core/formatter/MapboxDistanceUtilTest.kt | 459 +++++++++++++++++- 6 files changed, 520 insertions(+), 10 deletions(-) create mode 100644 changelog/unreleased/bugfixes/dd2.md diff --git a/changelog/unreleased/bugfixes/dd2.md b/changelog/unreleased/bugfixes/dd2.md new file mode 100644 index 00000000000..8e85f307639 --- /dev/null +++ b/changelog/unreleased/bugfixes/dd2.md @@ -0,0 +1 @@ +- Made default rounding increment in `DistanceFormatterOptions` dependent on distance numerical value. \ No newline at end of file diff --git a/libnavigation-base/api/current.txt b/libnavigation-base/api/current.txt index 1e1d7afe0ff..aa0efbb06a4 100644 --- a/libnavigation-base/api/current.txt +++ b/libnavigation-base/api/current.txt @@ -63,10 +63,11 @@ package com.mapbox.navigation.base.formatter { field public static final int INCREMENT_ONE_HUNDRED = 100; // 0x64 field public static final int INCREMENT_TEN = 10; // 0xa field public static final int INCREMENT_TWENTY_FIVE = 25; // 0x19 + field public static final int INCREMENT_UNDEFINED = -1; // 0xffffffff field public static final com.mapbox.navigation.base.formatter.Rounding INSTANCE; } - @IntDef({com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TEN, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TWENTY_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIFTY, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_ONE_HUNDRED}) @kotlin.annotation.Retention(kotlin.annotation.AnnotationRetention.BINARY) public static @interface Rounding.Increment { + @IntDef({com.mapbox.navigation.base.formatter.Rounding.INCREMENT_UNDEFINED, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TEN, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TWENTY_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIFTY, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_ONE_HUNDRED}) @kotlin.annotation.Retention(kotlin.annotation.AnnotationRetention.BINARY) public static @interface Rounding.Increment { } public enum UnitType { diff --git a/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/DistanceFormatterOptions.kt b/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/DistanceFormatterOptions.kt index 2201dfafeb7..9d78303d622 100644 --- a/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/DistanceFormatterOptions.kt +++ b/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/DistanceFormatterOptions.kt @@ -78,7 +78,7 @@ class DistanceFormatterOptions private constructor( private val applicationContext: Context = applicationContext.applicationContext private var locale: Locale = applicationContext.inferDeviceLocale() private var unitType: UnitType? = null - private var roundingIncrement = Rounding.INCREMENT_FIFTY + private var roundingIncrement = Rounding.INCREMENT_UNDEFINED /** * Policy for the various units of measurement. diff --git a/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/Rounding.kt b/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/Rounding.kt index 7223e73f95f..47c33d7946e 100644 --- a/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/Rounding.kt +++ b/libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/Rounding.kt @@ -6,6 +6,12 @@ import androidx.annotation.IntDef * Rounding */ object Rounding { + + /** + * Undefined rounding increment. + */ + const val INCREMENT_UNDEFINED = -1 + /** * Rounding increment 5 * @@ -46,6 +52,7 @@ object Rounding { */ @Retention(AnnotationRetention.BINARY) @IntDef( + INCREMENT_UNDEFINED, INCREMENT_FIVE, INCREMENT_TEN, INCREMENT_TWENTY_FIVE, diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtil.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtil.kt index d9f31d8e533..dde1438ce19 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtil.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtil.kt @@ -3,6 +3,7 @@ package com.mapbox.navigation.core.formatter import android.content.Context import android.content.res.Configuration import android.content.res.Resources +import com.mapbox.navigation.base.formatter.Rounding import com.mapbox.navigation.base.formatter.UnitType import com.mapbox.navigation.core.R import com.mapbox.turf.TurfConstants @@ -16,6 +17,7 @@ import kotlin.math.roundToInt */ object MapboxDistanceUtil { + private const val INVALID_ROUNDING_INCREMENT = 50 private val enLanguage = Locale("en").language /** @@ -84,12 +86,28 @@ object MapboxDistanceUtil { distanceInMeters !in 0.0..Double.MAX_VALUE -> smallValue( 0.0, roundingIncrement, + INVALID_ROUNDING_INCREMENT, + TurfConstants.UNIT_METERS, + UnitType.METRIC + ) + distanceInMeters < 25.0 -> smallValue( + distanceInMeters, + roundingIncrement, + 5, + TurfConstants.UNIT_METERS, + UnitType.METRIC + ) + distanceInMeters < 100 -> smallValue( + distanceInMeters, + roundingIncrement, + 25, TurfConstants.UNIT_METERS, UnitType.METRIC ) distanceInMeters < 1000.0 -> smallValue( distanceInMeters, roundingIncrement, + 50, TurfConstants.UNIT_METERS, UnitType.METRIC ) @@ -128,6 +146,7 @@ object MapboxDistanceUtil { distanceInMiles !in 0.0..Double.MAX_VALUE -> smallValue( 0.0, roundingIncrement, + INVALID_ROUNDING_INCREMENT, TurfConstants.UNIT_YARDS, UnitType.IMPERIAL ) @@ -137,12 +156,29 @@ object MapboxDistanceUtil { TurfConstants.UNIT_MILES, TurfConstants.UNIT_YARDS ) - smallValue( - distanceInYards, - roundingIncrement, - TurfConstants.UNIT_YARDS, - UnitType.IMPERIAL - ) + when { + distanceInYards < 20 -> smallValue( + distanceInYards, + roundingIncrement, + 10, + TurfConstants.UNIT_YARDS, + UnitType.IMPERIAL + ) + distanceInYards < 100 -> smallValue( + distanceInYards, + roundingIncrement, + 25, + TurfConstants.UNIT_YARDS, + UnitType.IMPERIAL + ) + else -> smallValue( + distanceInYards, + roundingIncrement, + 50, + TurfConstants.UNIT_YARDS, + UnitType.IMPERIAL + ) + } } distanceInMiles < 3.0 -> largeValue( distanceInMiles, @@ -170,6 +206,7 @@ object MapboxDistanceUtil { distanceInMiles !in 0.0..Double.MAX_VALUE -> smallValue( 0.0, roundingIncrement, + INVALID_ROUNDING_INCREMENT, TurfConstants.UNIT_FEET, UnitType.IMPERIAL ) @@ -182,6 +219,7 @@ object MapboxDistanceUtil { smallValue( distanceInFeet, roundingIncrement, + 50, TurfConstants.UNIT_FEET, UnitType.IMPERIAL ) @@ -206,12 +244,18 @@ object MapboxDistanceUtil { private fun smallValue( distance: Double, roundingIncrement: Int, + defaultRoundingIncrement: Int, unitTypeString: String, unitType: UnitType ): FormattingData { + val inferredRoundingIncrement = if (roundingIncrement == Rounding.INCREMENT_UNDEFINED) { + defaultRoundingIncrement + } else { + roundingIncrement + } val roundedValue = roundSmallDistance( distance, - roundingIncrement, + inferredRoundingIncrement, ) return FormattingData( roundedValue.toDouble(), diff --git a/libnavigation-core/src/test/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtilTest.kt b/libnavigation-core/src/test/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtilTest.kt index ba7c8c7b845..b49a2c204a5 100644 --- a/libnavigation-core/src/test/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtilTest.kt +++ b/libnavigation-core/src/test/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtilTest.kt @@ -39,6 +39,22 @@ class MapboxDistanceUtilTest { assertEquals(12.0, result.distance, 0.1) } + @Config(qualifiers = "en") + @Test + fun `formatDistance large value imperial with undefined rounding increment`() { + val result = MapboxDistanceUtil.formatDistance( + 19312.1, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("12", result.distanceAsString) + assertEquals("mi", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(12.0, result.distance, 0.1) + } + @Config(qualifiers = "en") @Test fun `formatDistance large at lower bound value imperial with default locale`() { @@ -71,6 +87,22 @@ class MapboxDistanceUtilTest { assertEquals(12.0, result.distance, 0.1) } + @Config(qualifiers = "en") + @Test + fun `formatDistance large value UK with undefined rounding increment`() { + val result = MapboxDistanceUtil.formatDistance( + 19312.1, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("12", result.distanceAsString) + assertEquals("mi", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(12.0, result.distance, 0.1) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance large value UK at lower bound with default locale`() { @@ -103,6 +135,22 @@ class MapboxDistanceUtilTest { assertEquals(19.3121, result.distance, 0.0) } + @Config(qualifiers = "en") + @Test + fun `formatDistance large value metric with undefined rounding increment`() { + val result = MapboxDistanceUtil.formatDistance( + 19312.1, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ctx + ) + + assertEquals("19", result.distanceAsString) + assertEquals("km", result.distanceSuffix) + assertEquals(UnitType.METRIC, result.unitType) + assertEquals(19.3121, result.distance, 0.0) + } + @Config(qualifiers = "en") @Test fun `formatDistance large value metric at lower bound with default locale`() { @@ -151,6 +199,53 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result.distance, 0.0) } + @Config(qualifiers = "en") + @Test + fun `formatDistance small value metric with default increment = 5`() { + val result = MapboxDistanceUtil.formatDistance( + 16.3, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ctx + ) + + assertEquals("15", result.distanceAsString) + assertEquals("m", result.distanceSuffix) + assertEquals(UnitType.METRIC, result.unitType) + assertEquals(15.0, result.distance, 0.0) + } + + @Test + fun `formatDistance small value metric with default increment = 25`() { + val result = MapboxDistanceUtil.formatDistance( + 76.3, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ctx + ) + + assertEquals("75", result.distanceAsString) + assertEquals("m", result.distanceSuffix) + assertEquals(UnitType.METRIC, result.unitType) + assertEquals(75.0, result.distance, 0.0) + } + + @Config(qualifiers = "en") + @Test + fun `formatDistance small value metric with default increment = 50`() { + val result = MapboxDistanceUtil.formatDistance( + 927.3, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ctx + ) + + assertEquals("900", result.distanceAsString) + assertEquals("m", result.distanceSuffix) + assertEquals(UnitType.METRIC, result.unitType) + assertEquals(900.0, result.distance, 0.0) + } + @Config(qualifiers = "en") @Test fun `formatDistance small value metric at lower bound with default locale`() { @@ -215,6 +310,22 @@ class MapboxDistanceUtilTest { assertEquals(30.0, result.distance, 0.1) } + @Config(qualifiers = "en") + @Test + fun `formatDistance small value imperial with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 118.0, // 387 feet + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("350", result.distanceAsString) + assertEquals("ft", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(350.0, result.distance, 0.1) + } + @Config(qualifiers = "en") @Test fun `formatDistance small value imperial at lower bound with default locale`() { @@ -262,6 +373,54 @@ class MapboxDistanceUtilTest { assertEquals(110.0, result.distance, 0.1) } + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance small value UK with default increment = 10`() { + val result = MapboxDistanceUtil.formatDistance( + 17.4, // 19 yards + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("10", result.distanceAsString) + assertEquals("yd", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(10.0, result.distance, 0.1) + } + + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance small value UK with default increment = 25`() { + val result = MapboxDistanceUtil.formatDistance( + 79.5, // 87 yards + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("75", result.distanceAsString) + assertEquals("yd", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(75.0, result.distance, 0.1) + } + + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance small value UK with default increment = 50`() { + val result = MapboxDistanceUtil.formatDistance( + 116.0, // 127 yards + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("100", result.distanceAsString) + assertEquals("yd", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(100.0, result.distance, 0.1) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance small value at upper bound UK with default locale`() { @@ -343,6 +502,22 @@ class MapboxDistanceUtilTest { assertEquals(1.1005, result.distance, 0.0) } + @Config(qualifiers = "en") + @Test + fun `formatDistance medium fractional value metric with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 1100.5, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ctx + ) + + assertEquals("1.1", result.distanceAsString) + assertEquals("km", result.distanceSuffix) + assertEquals(UnitType.METRIC, result.unitType) + assertEquals(1.1005, result.distance, 0.0) + } + @Config(qualifiers = "en") @Test fun `formatDistance medium value at upper bound metric with default locale`() { @@ -423,6 +598,22 @@ class MapboxDistanceUtilTest { assertEquals(9.3205679, result.distance, 0.00001) } + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance medium value UK with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 15000.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("9", result.distanceAsString) + assertEquals("mi", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(9.3205679, result.distance, 0.00001) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance medium value at lower bound UK with default locale`() { @@ -471,6 +662,22 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result.distance, 0.0) } + @Config(qualifiers = "en") + @Test + fun `formatDistance invalid value imperial with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + -19312.1, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("50", result.distanceAsString) + assertEquals("ft", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(50.0, result.distance, 0.0) + } + @Config(qualifiers = "en") @Test fun `formatDistance invalid medium value imperial with default locale`() { @@ -519,6 +726,22 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result.distance, 0.0) } + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance invalid large value UK with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + -19312.1, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ctx + ) + + assertEquals("50", result.distanceAsString) + assertEquals("yd", result.distanceSuffix) + assertEquals(UnitType.IMPERIAL, result.unitType) + assertEquals(50.0, result.distance, 0.0) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance invalid medium value UK with default locale`() { @@ -567,6 +790,22 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result.distance, 0.0) } + @Config(qualifiers = "en") + @Test + fun `formatDistance invalid large value metric with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + -19312.1, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ctx + ) + + assertEquals("50", result.distanceAsString) + assertEquals("m", result.distanceSuffix) + assertEquals(UnitType.METRIC, result.unitType) + assertEquals(50.0, result.distance, 0.0) + } + @Config(qualifiers = "en") @Test fun `formatDistance invalid medium value metric with default locale`() { @@ -610,6 +849,17 @@ class MapboxDistanceUtilTest { assertEquals(30.0, result, 0.0) } + @Test + fun `formatDistance imperial with undefined increment return small distance only`() { + val result = MapboxDistanceUtil.formatDistance( + 23.1, // 76 feet + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(50.0, result, 0.0) + } + @Test fun `formatDistance imperial return small at lower bound distance only`() { val result = MapboxDistanceUtil.formatDistance( @@ -656,6 +906,42 @@ class MapboxDistanceUtilTest { assertEquals(110.0, result, 0.0) } + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance UK return small distance only with default increment = 10`() { + val result = MapboxDistanceUtil.formatDistance( + 17.4, // 19 yards + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ) + + assertEquals(10.0, result, 0.1) + } + + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance UK return small distance only with default increment = 25`() { + val result = MapboxDistanceUtil.formatDistance( + 79.5, // 87 yards + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ) + + assertEquals(75.0, result, 0.1) + } + + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance UK return small distance only with default increment = 50`() { + val result = MapboxDistanceUtil.formatDistance( + 116.0, // 127 yards + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL, + ) + + assertEquals(100.0, result, 0.1) + } + @Test @Config(qualifiers = "en-rGB") fun `formatDistance UK return small at lower bound distance only`() { @@ -703,6 +989,17 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result, 0.0) } + @Test + fun `formatDistance imperial return small distance only for negative with undefined inc`() { + val result = MapboxDistanceUtil.formatDistance( + -10.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(50.0, result, 0.0) + } + @Test @Config(qualifiers = "en-rGB") fun `formatDistance UK return small distance only when input negative`() { @@ -715,6 +1012,18 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result, 0.0) } + @Test + @Config(qualifiers = "en-rGB") + fun `formatDistance UK return small distance only for negative with undefined inc`() { + val result = MapboxDistanceUtil.formatDistance( + -10.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(50.0, result, 0.0) + } + @Test fun `formatDistance imperial return medium distance only`() { val result = MapboxDistanceUtil.formatDistance( @@ -726,6 +1035,17 @@ class MapboxDistanceUtilTest { assertEquals(8.0778255, result, 0.00001) } + @Test + fun `formatDistance imperial return medium distance only with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 13000.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(8.0778255, result, 0.00001) + } + @Test fun `formatDistance imperial return medium at lower bound distance only`() { val result = MapboxDistanceUtil.formatDistance( @@ -760,6 +1080,18 @@ class MapboxDistanceUtilTest { assertEquals(6.49724, result, 0.00001) } + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance UK return medium distance only with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 10456.3, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(6.49724, result, 0.00001) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance UK return medium at lower bound distance only`() { @@ -806,6 +1138,17 @@ class MapboxDistanceUtilTest { assertEquals(6.7108112, result, 0.000001) } + @Test + fun `formatDistance imperial return large distance only with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 10800.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(6.7108112, result, 0.000001) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance UK return large distance only`() { @@ -818,6 +1161,18 @@ class MapboxDistanceUtilTest { assertEquals(6.7108089, result, 0.00001) } + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance UK return large distance only with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 10800.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(6.7108089, result, 0.00001) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance UK return large at lower bound distance only`() { @@ -831,7 +1186,7 @@ class MapboxDistanceUtilTest { } @Test - fun `formatDistance imperial invalid large metric returns fifty`() { + fun `formatDistance invalid large metric returns fifty`() { val result = MapboxDistanceUtil.formatDistance( -15000.0, Rounding.INCREMENT_FIFTY, @@ -841,6 +1196,17 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result, 0.0) } + @Test + fun `formatDistance invalid metric with undefined increment returns fifty`() { + val result = MapboxDistanceUtil.formatDistance( + -15000.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC + ) + + assertEquals(50.0, result, 0.0) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance UK invalid large returns fifty`() { @@ -853,6 +1219,18 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result, 0.0) } + @Config(qualifiers = "en-rGB") + @Test + fun `formatDistance UK invalid with undefined increment returns fifty`() { + val result = MapboxDistanceUtil.formatDistance( + -15000.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(50.0, result, 0.0) + } + @Test fun `formatDistance invalid medium metric returns fifty`() { val result = MapboxDistanceUtil.formatDistance( @@ -875,6 +1253,17 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result, 0.0) } + @Test + fun `formatDistance invalid imperial with undefined increment returns fifty`() { + val result = MapboxDistanceUtil.formatDistance( + -1500.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.IMPERIAL + ) + + assertEquals(50.0, result, 0.0) + } + @Config(qualifiers = "en-rGB") @Test fun `formatDistance invalid medium UK returns fifty`() { @@ -932,6 +1321,41 @@ class MapboxDistanceUtilTest { assertEquals(120.0, result, 0.0) } + @Config(qualifiers = "en") + @Test + fun `formatDistance metric return small distance only with default increment = 5`() { + val result = MapboxDistanceUtil.formatDistance( + 16.3, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ) + + assertEquals(15.0, result, 0.0) + } + + @Test + fun `formatDistance metric return small distance only with default increment = 25`() { + val result = MapboxDistanceUtil.formatDistance( + 76.3, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ) + + assertEquals(75.0, result, 0.0) + } + + @Config(qualifiers = "en") + @Test + fun `formatDistance metric return small distance only with default increment = 50`() { + val result = MapboxDistanceUtil.formatDistance( + 927.3, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC, + ) + + assertEquals(900.0, result, 0.0) + } + @Test fun `formatDistance metric return small at lower bound distance only`() { val result = MapboxDistanceUtil.formatDistance( @@ -965,6 +1389,17 @@ class MapboxDistanceUtilTest { assertEquals(50.0, result, 0.0) } + @Test + fun `formatDistance metric return small distance only for negative with undefined inc`() { + val result = MapboxDistanceUtil.formatDistance( + -10.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC + ) + + assertEquals(50.0, result, 0.0) + } + @Test fun `formatDistance metric return small distance only with custom locale`() { val result = MapboxDistanceUtil.formatDistance( @@ -988,6 +1423,17 @@ class MapboxDistanceUtilTest { assertEquals(2.367354, result, 0.00001) } + @Test + fun `formatDistance metric return medium distance only with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 2367.354, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC + ) + + assertEquals(2.367354, result, 0.00001) + } + @Test fun `formatDistance metric return medium at lower bound distance only`() { val result = MapboxDistanceUtil.formatDistance( @@ -1031,4 +1477,15 @@ class MapboxDistanceUtilTest { assertEquals(10.8, result, 0.0) } + + @Test + fun `formatDistance metric return large distance only with undefined increment`() { + val result = MapboxDistanceUtil.formatDistance( + 10800.0, + Rounding.INCREMENT_UNDEFINED, + UnitType.METRIC + ) + + assertEquals(10.8, result, 0.0) + } }