gpt4 book ai didi

Scala 案例类测试

转载 作者:行者123 更新时间:2023-11-28 21:10:51 25 4
gpt4 key购买 nike

我用案例类 Point 和 LineSegment 定义了一个密封的特征形状,并且有一个 LineSegment 类的行为,它告诉我两个 LineSegment 是否相交,但是当我尝试测试该行为时..它说它无法在测试套件。

sealed trait Shape
case class Point(x: Int, y: Int) extends Shape {
//require(x != null & y !=null, "null values for point")
}

case class LineSegment(point1: Point, point2: Point) extends Shape {
require(point1 != point2)

def intersect(lineSegment1: LineSegment, lineSegment2:LineSegment):
Boolean ={
// val A1, A2, B1, B2, C1, C2
val A1 = lineSegment1.point2.y - lineSegment1.point1.y
val B1 = lineSegment1.point1.x - lineSegment1.point2.x
val C1 = A1 * lineSegment1.point1.x + B1 * lineSegment1.point1.y

val A2 = lineSegment2.point2.y - lineSegment2.point1.y
val B2 = lineSegment2.point1.x - lineSegment2.point2.x
val C2 = A2 * lineSegment2.point1.x + B2 * lineSegment2.point1.y

val det = A1 * B2 - A2 * B1
if(det == 0)
return false
else
return true

}

}

这是我要做的测试

import model.Point
import model.LineSegment
import org.scalatest.{FlatSpec, ShouldMatchers}

class LineIntersectionTest extends FlatSpec with ShouldMatchers{
val point1 = Point(20,30);
val point2 = Point(10,35);
val point3 = Point(100,10);
val point4 = Point(16,45);

val lineSegment1 = LineSegment(point1,point2);
val lineSegment2 = LineSegment(point3,point4);

"LineSegmentIntersection Behaviour" should "work for a line segment"
in {
intersect(lineSegment1, lineSegment2) should be (true)
}}

但我得到一个错误无法解析符号相交,无法解析符号应该..它不识别方法相交

我在这里做错了什么?

最佳答案

您正在使用 intersect,就好像它是 LineIntersectionTest 类上的函数一样。以下应该有效:

lineSegment1.intersect(lineSegment1, lineSegment2) should be (true)

但是我可能会使用不同的实现方式。也许:

object LineUtils {
def intersect(l1: LineSegment, l2: LineSegment): Boolean
}

// Usage
LineUtils.insersect(lineSegment1, lineSegment2)

关于Scala 案例类测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28642785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com