工作代码在ScalaTest中提供ClassCastException

时间:2018-06-06 07:09:21

标签: scala scalatest

我正在尝试使用给定的 code-snippet 来实现这三件事:

  • 删除重复项(来自传递列表longSeq
  • 丢弃违反边界的元素(loBound < x < hiBound - 1
  • 对结果列表进行排序

代码段:

val loBoung: Long = 0L
val hiBound: Long = Long.MaxValue
val longSeq: Seq[Long] = ...
...
val sortedTrimmedLongSeq: Seq[Long] =
  longSeq.toSet.toList.
  filter { p: Long => (loBound < p) && (p < hiBound - 1L) }.
  sorted

指定的代码段适用于Scala 工作表,但会在单元测试中抛出以下堆栈跟踪({{1行是上面代码段中包含MyClass.scala:104操作的行:

filter

是因为MyClassTest: [info] - makeContinuousRanges : converts sequence of big-ints to range segments *** FAILED *** [info] java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to java.lang.Long [info] at scala.runtime.BoxesRunTime.unboxToLong(BoxesRunTime.java:105) [info] at com.company.spark.sql.utils.MyClass$$anonfun$3.apply(MyClass.scala:104) [info] at scala.collection.TraversableLike$$anonfun$filterImpl$1.apply(TraversableLike.scala:248) [info] at scala.collection.immutable.List.foreach(List.scala:392) [info] at scala.collection.TraversableLike$class.filterImpl(TraversableLike.scala:247) [info] at scala.collection.TraversableLike$class.filter(TraversableLike.scala:259) [info] at scala.collection.AbstractTraversable.filter(Traversable.scala:104) [info] at com.company.spark.sql.utils.MyClass$.makeContinuousRanges(MyClass.scala:104) [info] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [info] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [info] ... [info] ScalaTest 还是存在合法错误?

UPDATE-1

根据 @Dmytro Mitin 的建议,我现在提供更多详情。

我正在尝试将scalatest.FunSuite的列表转换为连续范围作为Long(但是较低和上限不能相等;有其他限制,请参阅下面的代码。)

  

(假设为range[i] = (seqLong[i] + 1, seqLong[i + 1])&amp; loBound = 0

     

输入-1:[5,10,15]

     

输出-1:[(0,5),(6,10),(11,15),(16,Long.MaxValue)]

           

输入-2:[5,10,12,20,20]

     

输出-2:[(0,5),(6,10),(11,12),(13,20),(21,Long.MaxValue)]

           

输入-2:[5,10,11,12,13,20]]

     

输出-2:[(0,5),(6,13),(14,20),(21,Long.MaxValue)]

下面显示的是我为此目的编写的函数,其中包含有问题的代码段

hiBound = Long.MaxValue

上述方法位于def makeContinuousRanges(longSeq: Seq[Long], loBound: Long = 0L, hiBound: Long = Long.MaxValue ): Seq[(Long, Long)] = { // [1] remove duplicates [2] fix bounds [3] sort val sortedTrimmedLongSeq: Seq[Long] = longSeq.toSet.toList. filter { p: Long => (loBound < p) && (p < hiBound - 1L) }. sorted // [1] create ranges [2] ensure that for all ranges [lo, hi], lo != hi val rangeSeq: Seq[(Long, Long)] = sortedTrimmedLongSeq match { case Nil => Seq.empty[(Long, Long)] case head :: Nil => Seq((loBound, head), (head + 1L, hiBound)) case _ => { val continuousWithoutFirstWithoutLast: Seq[(Long, Long)] = sortedTrimmedLongSeq.zip(sortedTrimmedLongSeq.tail). map(tuple => (tuple._1 + 1L, tuple._2)) val continuousWithFirstWithoutLast: Seq[(Long, Long)] = (loBound, sortedTrimmedLongSeq.head) +: continuousWithoutFirstWithoutLast val continuousWithFirstWithLast: Seq[(Long, Long)] = continuousWithFirstWithoutLast :+ (sortedTrimmedLongSeq.last + 1L, hiBound) continuousWithFirstWithLast }.filterNot(tup => tup._1 == tup._2) } // fill up any 'holes' created in the range due to that 'filterNot' step val smoothRangeSeqWithoutLast: Seq[(Long, Long)] = rangeSeq.zip(rangeSeq.tail). map { tup: ((Long, Long), (Long, Long)) => (tup._1._1, tup._2._1 - 1L) } val smoothRangeSeqWithLast: Seq[(Long, Long)] = smoothRangeSeqWithoutLast :+ rangeSeq.last smoothRangeSeqWithLast } 内,比如说Object。就像我之前说的那样,我已经在MyClass 工作表中单独测试了该功能。但是在单元测试中它是错误的(在包含Scala的行上)。

我正在使用

  • filter
  • Scala v2.11.11
  • SBT v1.0.3

0 个答案:

没有答案