/geomesa-filter/src/test/scala/org/locationtech/geomesa/filter/FilterBoundsTest.scala

https://gitlab.com/zachcoyle/geomesa · Scala · 142 lines · 127 code · 8 blank · 7 comment · 0 complexity · dadce9c3d48ecab5ec033a63c59c664b MD5 · raw file

  1. /***********************************************************************
  2. * Copyright (c) 2013-2016 Commonwealth Computer Research, Inc.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Apache License, Version 2.0
  5. * which accompanies this distribution and is available at
  6. * http://www.opensource.org/licenses/apache2.0.php.
  7. *************************************************************************/
  8. package org.locationtech.geomesa.filter
  9. import java.util.{Date, UUID}
  10. import org.junit.runner.RunWith
  11. import org.specs2.mutable.Specification
  12. import org.specs2.runner.JUnitRunner
  13. @RunWith(classOf[JUnitRunner])
  14. class FilterBoundsTest extends Specification {
  15. "FilterBounds" should {
  16. "merge different types" >> {
  17. "ints" >> {
  18. val leftLower: Option[java.lang.Integer] = Some(0)
  19. val leftUpper: Option[java.lang.Integer] = Some(10)
  20. val rightLower: Option[java.lang.Integer] = Some(5)
  21. val rightUpper: Option[java.lang.Integer] = Some(15)
  22. val left = FilterBounds("", Seq(Bounds(leftLower, leftUpper, inclusive = true)))
  23. val right = FilterBounds("", Seq(Bounds(rightLower, rightUpper, inclusive = true)))
  24. left.and(right).bounds mustEqual Seq(Bounds(rightLower, leftUpper, inclusive = true))
  25. left.or(right).bounds mustEqual Seq(Bounds(leftLower, rightUpper, inclusive = true))
  26. }
  27. "longs" >> {
  28. val leftLower: Option[java.lang.Long] = Some(0L)
  29. val leftUpper: Option[java.lang.Long] = Some(10L)
  30. val rightLower: Option[java.lang.Long] = Some(5L)
  31. val rightUpper: Option[java.lang.Long] = Some(15L)
  32. val left = FilterBounds("", Seq(Bounds(leftLower, leftUpper, inclusive = true)))
  33. val right = FilterBounds("", Seq(Bounds(rightLower, rightUpper, inclusive = true)))
  34. left.and(right).bounds mustEqual Seq(Bounds(rightLower, leftUpper, inclusive = true))
  35. left.or(right).bounds mustEqual Seq(Bounds(leftLower, rightUpper, inclusive = true))
  36. }
  37. "floats" >> {
  38. val leftLower: Option[java.lang.Float] = Some(0f)
  39. val leftUpper: Option[java.lang.Float] = Some(10f)
  40. val rightLower: Option[java.lang.Float] = Some(5f)
  41. val rightUpper: Option[java.lang.Float] = Some(15f)
  42. val left = FilterBounds("", Seq(Bounds(leftLower, leftUpper, inclusive = true)))
  43. val right = FilterBounds("", Seq(Bounds(rightLower, rightUpper, inclusive = true)))
  44. left.and(right).bounds mustEqual Seq(Bounds(rightLower, leftUpper, inclusive = true))
  45. left.or(right).bounds mustEqual Seq(Bounds(leftLower, rightUpper, inclusive = true))
  46. }
  47. "doubles" >> {
  48. val leftLower: Option[java.lang.Double] = Some(0d)
  49. val leftUpper: Option[java.lang.Double] = Some(10d)
  50. val rightLower: Option[java.lang.Double] = Some(5d)
  51. val rightUpper: Option[java.lang.Double] = Some(15d)
  52. val left = FilterBounds("", Seq(Bounds(leftLower, leftUpper, inclusive = true)))
  53. val right = FilterBounds("", Seq(Bounds(rightLower, rightUpper, inclusive = true)))
  54. left.and(right).bounds mustEqual Seq(Bounds(rightLower, leftUpper, inclusive = true))
  55. left.or(right).bounds mustEqual Seq(Bounds(leftLower, rightUpper, inclusive = true))
  56. }
  57. "strings" >> {
  58. val leftLower: Option[String] = Some("0")
  59. val leftUpper: Option[String] = Some("6")
  60. val rightLower: Option[String] = Some("3")
  61. val rightUpper: Option[String] = Some("9")
  62. val left = FilterBounds("", Seq(Bounds(leftLower, leftUpper, inclusive = true)))
  63. val right = FilterBounds("", Seq(Bounds(rightLower, rightUpper, inclusive = true)))
  64. left.and(right).bounds mustEqual Seq(Bounds(rightLower, leftUpper, inclusive = true))
  65. left.or(right).bounds mustEqual Seq(Bounds(leftLower, rightUpper, inclusive = true))
  66. }
  67. "dates" >> {
  68. val leftLower: Option[Date] = Some(new Date(0))
  69. val leftUpper: Option[Date] = Some(new Date(10))
  70. val rightLower: Option[Date] = Some(new Date(5))
  71. val rightUpper: Option[Date] = Some(new Date(15))
  72. val left = FilterBounds("", Seq(Bounds(leftLower, leftUpper, inclusive = true)))
  73. val right = FilterBounds("", Seq(Bounds(rightLower, rightUpper, inclusive = true)))
  74. left.and(right).bounds mustEqual Seq(Bounds(rightLower, leftUpper, inclusive = true))
  75. left.or(right).bounds mustEqual Seq(Bounds(leftLower, rightUpper, inclusive = true))
  76. }
  77. "uuids" >> {
  78. val leftLower: Option[UUID] = Some(UUID.fromString("00000000-0000-0000-0000-000000000000"))
  79. val leftUpper: Option[UUID] = Some(UUID.fromString("00000000-0000-0000-0000-000000000006"))
  80. val rightLower: Option[UUID] = Some(UUID.fromString("00000000-0000-0000-0000-000000000003"))
  81. val rightUpper: Option[UUID] = Some(UUID.fromString("00000000-0000-0000-0000-000000000009"))
  82. val left = FilterBounds("", Seq(Bounds(leftLower, leftUpper, inclusive = true)))
  83. val right = FilterBounds("", Seq(Bounds(rightLower, rightUpper, inclusive = true)))
  84. left.and(right).bounds mustEqual Seq(Bounds(rightLower, leftUpper, inclusive = true))
  85. left.or(right).bounds mustEqual Seq(Bounds(leftLower, rightUpper, inclusive = true))
  86. }
  87. }
  88. "merge simple ands/ors" >> {
  89. "for strings" >> {
  90. val bounds = FilterBounds("", Seq(Bounds(Some("b"), Some("f"), inclusive = true)))
  91. "overlapping" >> {
  92. val toMerge = FilterBounds("", Seq(Bounds(Some("d"), Some("i"), inclusive = true)))
  93. bounds.and(toMerge).bounds mustEqual Seq(Bounds(Some("d"), Some("f"), inclusive = true))
  94. bounds.or(toMerge).bounds mustEqual Seq(Bounds(Some("b"), Some("i"), inclusive = true))
  95. }
  96. "disjoint" >> {
  97. val toMerge = FilterBounds("", Seq(Bounds(Some("i"), Some("z"), inclusive = true)))
  98. bounds.and(toMerge).bounds must beEmpty
  99. bounds.or(toMerge).bounds mustEqual Seq(
  100. Bounds(Some("b"), Some("f"), inclusive = true),
  101. Bounds(Some("i"), Some("z"), inclusive = true)
  102. )
  103. }
  104. "contained" >> {
  105. val toMerge = FilterBounds("", Seq(Bounds(Some("c"), Some("d"), inclusive = true)))
  106. bounds.and(toMerge).bounds mustEqual Seq(Bounds(Some("c"), Some("d"), inclusive = true))
  107. bounds.or(toMerge).bounds mustEqual Seq(Bounds(Some("b"), Some("f"), inclusive = true))
  108. }
  109. "containing" >> {
  110. val toMerge = FilterBounds("", Seq(Bounds(Some("a"), Some("i"), inclusive = true)))
  111. bounds.and(toMerge).bounds mustEqual Seq(Bounds(Some("b"), Some("f"), inclusive = true))
  112. bounds.or(toMerge).bounds mustEqual Seq(Bounds(Some("a"), Some("i"), inclusive = true))
  113. }
  114. }
  115. }
  116. "merge complex ands/ors" >> {
  117. "for strings" >> {
  118. val bounds = FilterBounds("", Seq(Bounds(Some("b"), Some("f"), inclusive = true)))
  119. val or = bounds.or(FilterBounds("", Seq(Bounds(Some("i"), Some("m"), inclusive = true))))
  120. or.bounds mustEqual Seq(
  121. Bounds(Some("b"), Some("f"), inclusive = true),
  122. Bounds(Some("i"), Some("m"), inclusive = true)
  123. )
  124. val and = or.and(FilterBounds("", Seq(Bounds(Some("e"), Some("k"), inclusive = true))))
  125. and.bounds mustEqual Seq(
  126. Bounds(Some("e"), Some("f"), inclusive = true),
  127. Bounds(Some("i"), Some("k"), inclusive = true)
  128. )
  129. val or2 = and.or(FilterBounds("", Seq(Bounds(Some("f"), Some("i"), inclusive = true))))
  130. or2.bounds mustEqual Seq(Bounds(Some("e"), Some("k"), inclusive = true))
  131. }
  132. }
  133. }
  134. }