一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 1248|回复: 15
收起左侧

[CS61B_Spring 2015] Lab2 - JUnit Testing

[复制链接] |试试Instant~ |关注本帖
hurricane_e 发表于 2015-5-28 04:11:38 | 显示全部楼层 |阅读模式

[其他]CS61B Data Structure #14 - 2015-05-25@Berkeley

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
Rt~
whosays 发表于 2015-5-28 23:34:30 | 显示全部楼层
请教一个问题,一般加分帖有效多久?
回复 支持 反对

使用道具 举报

 楼主| hurricane_e 发表于 2015-5-29 01:56:14 | 显示全部楼层
whosays 发表于 2015-5-28 23:34
请教一个问题,一般加分帖有效多久?

一直有效呀~
回复 支持 反对

使用道具 举报

whosays 发表于 2015-5-29 06:05:48 | 显示全部楼层

辛苦了!
回复 支持 反对

使用道具 举报

 楼主| hurricane_e 发表于 2015-5-29 12:49:15 | 显示全部楼层
1. Arithmetic
Screenshot 2015-05-28 21.37.04.png
2. Triangle & Test

Screenshot 2015-05-28 21.39.03.png

Screenshot 2015-05-28 21.40.16.png

3. IntList

Screenshot 2015-05-28 21.41.58.png

All the tests:

Screenshot 2015-05-28 21.43.29.png



评分

1

查看全部评分

回复 支持 反对

使用道具 举报

niyanwen212 发表于 2015-5-29 23:34:20 | 显示全部楼层
小白求问: test的文件编译有错是怎么回事? 是要把@test 给注掉吗?
回复 支持 反对

使用道具 举报

 楼主| hurricane_e 发表于 2015-5-30 03:13:45 | 显示全部楼层
niyanwen212 发表于 2015-5-29 23:34
小白求问: test的文件编译有错是怎么回事? 是要把@test 给注掉吗?

课程里好像说到不能去掉@test
回复 支持 反对

使用道具 举报

niyanwen212 发表于 2015-5-30 05:24:40 | 显示全部楼层
hurricane_e 发表于 2015-5-30 03:13
课程里好像说到不能去掉@test 的

恩,看了lab1c后知道该怎么做了
回复 支持 反对

使用道具 举报

baiery 发表于 2015-6-1 13:02:07 | 显示全部楼层
楼主楼主,我又想问一下lab1c里说的 skeleton repository在哪里啊?我找不到这个文件夹,下不了里面的library,另外lab2里的说的三个java文件我也找不到,我觉得我要蠢死了。。。。
回复 支持 反对

使用道具 举报

 楼主| hurricane_e 发表于 2015-6-1 13:50:42 | 显示全部楼层
baiery 发表于 2015-6-1 13:02
楼主楼主,我又想问一下lab1c里说的 skeleton repository在哪里啊?我找不到这个文件夹,下不了里面的libra ...

别急别急 我是按照lab1的instruction一步一步做下来,然后一次性就可以把所有skeleton里面的文件都pull下来的,只要一次做完,那个文件夹里包含了所有lab/hw要用到的东西。。但是第一次pull的过程非常痛苦(因为啥都不会。。。)
回复 支持 反对

使用道具 举报

baiery 发表于 2015-6-2 09:10:12 | 显示全部楼层
1.Arithmetic.java
public class Arithmetic {
    /** Computes product of two ints */

    public static int product(int a, int b) {
        return a * b;
    }

    /** Computes sum of two ints (incorrectly) */

    public static int sum(int a, int b) {
        return a + b; //改了这里
    }   
}

2.
TriangleTest.java
mport org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author melaniecebula
*/
public class TriangleTest {
  /**  We've already created a testScalene method.  Please fill in testEquilateral, and additionally
   *   create tests for Isosceles, Negative Sides, and Invalid sides
   **/

    @Test
    public void testScalene() {
        Triangle t = new Triangle(30, 40, 50);
        String result = t.triangleType();
        assertEquals("Scalene", result);
    }

    @Test
    public void testEquilateral() {
        Triangle t = new Triangle(30, 30, 30);
        String result = t.triangleType();
        assertEquals("Equilateral", result);
    }

    @Test
    public void testNegativeSides() {
        Triangle t = new Triangle(-30, -30, 30);
        String result = t.triangleType();
        assertEquals("At least one length is less than 0!", result);
    }

    @Test
    public void testInvalidSides() {
        Triangle t = new Triangle(1, 2, 30);
        String result = t.triangleType();
        assertEquals("The lengths of the triangles do not form a valid triangle!", result);
    }

    @Test
    public void testIsosceles() {
        Triangle t = new Triangle(30, 30, 20);
        String result = t.triangleType();
        assertEquals("Isosceles", result);
    }
    public static void main(String[] args) {
        jh61b.junit.textui.runClasses(TriangleTest.class);
    }
}

Triangle.java

public class Triangle {

  int s1;
  int s2;
  int s3;

  public Triangle(int side1, int side2, int side3) {
    s1 = side1;
    s2 = side2;
    s3 = side3;
  }

  /** Returns a String describing the type of triangle. */
  public String triangleType() {

    // Chceck that nothing is negative
    if (s1 <= 0 || s2 <= 0 || s3 <= 0) {
      return "At least one length is less than 0!";
    }

    // Check for side length
    else if ((s1 + s2 <= s3) || (s1 + s3 <= s2) || (s2 + s3 <= s1)) {
      return "The lengths of the triangles do not form a valid triangle!";
    }  

    else if ((s1 == s2) && (s2== s3)){
      return "Equilateral";
    }

    else if ((s1 == s2) || (s1 == s3) || (s2 == s3)){
      return "Isosceles";
    }

    else{
      return "Scalene";
    }
  }
}

3.
public static IntList dcatenate(IntList A, IntList B) {
    //TODO:  fill in method
    while(A.tail != null){
      A = A.tail;
    }
    A.tail = B;
    return A;
  }

  /** Returns a list consisting of the elements of A followed by the
   ** elements of B.  May NOT modify items of A.  Use 'new'. */
  public static IntList catenate(IntList A, IntList B) {
    //TODO:  fill in method
    if (A == null) {
      return B;
    }
    return new IntList(A.head, catenate(A.tail, B));
  }
   
  }


@Test
    public void testDCatenate() {
      IntList A = IntList.list(1, 2, 3);
      IntList B = IntList.list(4, 5, 6);
       IntList L = IntList.dcatenate(A, B);
      assertEquals(IntList.list(1, 2,3,4,5,6), L);
    }

    @Test
    public void testCatenate() {
      IntList A = IntList.list(1, 2, 3);
      IntList B = IntList.list(4, 5, 6);
      IntList L = IntList.catenate(A, B);
      assertEquals(IntList.list(1, 2,3,4,5,6), L);
    }

四个均过了,就不贴test结果了,天啊我简直是拖延症晚期。。。。赶快把HW1做了

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

karte_polo 发表于 2015-6-4 21:44:17 | 显示全部楼层
1. Arithmetic.java

public class Arithmetic {
    /** Computes product of two ints */

    public static int product(int a, int b) {
        return a * b;
    }

    /** Computes sum of two ints (incorrectly) */

    public static int sum(int a, int b) {
        return a + b;
    }   
}

2 .triangle.java

增加了以下部分:
    if (s1==s2 && s1==s3){
            return "Equilateral";
    }else if (s1==s2 || s2==s3 || s1==s3){
            return "Isosceles";
    }else{
            return "Scalene";
    }

3.TriangleTest.java

增加了下述部分:
    @Test
    public void testEquilateral() {
        Triangle t = new Triangle(50, 50, 50);
        String result = t.triangleType();
        assertEquals("Equilateral", result);
    }
    @Test
    public void testIsosceles() {
        Triangle t = new Triangle(50, 50, 30);
        String result = t.triangleType();
        assertEquals("Isosceles", result);
    }
    @Test
    public void testNegative() {
        Triangle t = new Triangle(50, 50, -20);
        String result = t.triangleType();
        assertEquals("At least one length is less than 0!", result);
    }
    @Test
    public void testinvalid() {
        Triangle t = new Triangle(3, 4, 7);
        String result = t.triangleType();
        assertEquals("The lengths of the triangles do not form a valid triangle!", result);
    }

4.IntList.java

增加部分:
  public static IntList dcatenate(IntList A, IntList B) {
          IntList r = A;
          while (A.tail!=null){
                  A= A.tail;
          }
          A.tail = B;
          return r;

  }

  public static IntList catenate(IntList A, IntList B) {
        if (A == null){
                return B;
        }
        return new IntList(A.head,catenate(A.tail,B));
  }

5. IntListTest.java

增加部分:

    @Test
    public void testSquareListRecursive() {
      IntList L = IntList.list(1, 2, 3);
      IntList.dSquareList(L);
      assertEquals(IntList.list(1, 4, 9), L);
    }
    @Test
    public void testDcatenate() {
      IntList A = IntList.list(1, 2, 3);
      IntList B = IntList.list(4,5,6);
      IntList.dcatenate(A,B);
      assertEquals(IntList.list(1, 2, 3,4,5,6), A);
    }
   
    @Test
    public void testCatenate() {
      IntList A = IntList.list(1, 2, 3);
      IntList B = IntList.list(4,5,6);
      IntList L = IntList.catenate(A,B);
      assertEquals(IntList.list(1, 2, 3), A);
      assertEquals(IntList.list(1, 2, 3,4,5,6), L);
    }

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

a0106660 发表于 2015-6-13 15:33:20 | 显示全部楼层
来交作业了:
IntList.java:
import java.util.Formatter;

/** Scheme-like pairs that can be used to form a list of integers.
*  @author P. N. Hilfinger, with some modifications by Josh Hug and melaniecebula
*  [Do not modify this file.]
*/
public class IntList {
  /** First element of list. */
  public int head;
  /** Remaining elements of list. */
  public IntList tail;

  /** A List with head HEAD0 and tail TAIL0. */
  public IntList(int head0, IntList tail0) {
    head = head0;
    tail = tail0;
  }

  /** A List with null tail, and head = 0. */
  public IntList() {
    /* NOTE: public IntList () { }  would also work. */
    this(0, null);
  }

  /** Returns a list equal to L with all elements squared. Destructive. */
  public static void dSquareList(IntList L) {

    while (L != null) {
      L.head = L.head * L.head;
      L = L.tail;
    }
  }

  /** Returns a list equal to L with all elements squared. Non-destructive. */
  public static IntList squareListIterative(IntList L) {
    if (L == null) {
      return null;
    }
    IntList res = new IntList(L.head * L.head, null);
    IntList ptr = res;
    L = L.tail;
    while (L != null) {
      ptr.tail = new IntList(L.head * L.head, null);
      L = L.tail;
      ptr = ptr.tail;
    }
    return res;
  }

  /** Returns a list equal to L with all elements squared. Non-destructive. */
  public static IntList squareListRecursive(IntList L) {
    if (L == null) {
      return null;
    }
    return new IntList(L.head * L.head, squareListRecursive(L.tail));
  }

  /** DO NOT MODIFY ANYTHING ABOVE THIS LINE! */


  /** Returns a list consisting of the elements of A followed by the
   **  elements of B.  May modify items of A. Don't use 'new'. */

  public static IntList dcatenate(IntList A, IntList B) {
    //TODO:  fill in method
    IntList a = A;
    while(a.tail!=null){
      a = a.tail;
    }
    a.tail = B;
    return A;
  }

  /** Returns a list consisting of the elements of A followed by the
   ** elements of B.  May NOT modify items of A.  Use 'new'. */
  public static IntList catenate(IntList A, IntList B) {
    //TODO:  fill in method
    IntList a = A;
    IntList b = B;
    IntList temp = null;
    IntList newlist = null;
    if(A==null){
      return B;
    }
    newlist = new IntList(A.head,null);
    temp = newlist;
    a = a.tail;
    while(a!=null){
      temp.tail = new IntList(a.head,null);
      temp = temp.tail;
      a = a.tail;
    }

    while(b!=null){
      temp.tail = new IntList(b.head,null);
      temp = temp.tail;
      b = b.tail;
    }
    return newlist;
  }

























  /** DO NOT MODIFY ANYTHING BELOW THIS LINE! In fact, I wouldn't even
    * look below this line since it's likely to confuse you. */

  @Override
  public int hashCode() {
    return head;
  }

  /** Returns a new IntList containing the ints in ARGS. You are not
    * expected to read or understand this method. */
  public static IntList list(Integer ... args) {
    IntList result, p;

    if (args.length > 0) {
      result = new IntList(args[0], null);
    } else {
      return null;
    }

    int k;
    for (k = 1, p = result; k < args.length; k += 1, p = p.tail) {
      p.tail = new IntList(args[k], null);
    }
    return result;
  }

  /** Returns true iff X is an IntList containing the same sequence of ints
   *  as THIS. Cannot handle IntLists with cycles. You are not expected to
   *  read or understand this method. */
  public boolean equals(Object x) {
    if (!(x instanceof IntList)) {
      return false;
    }
    IntList L = (IntList) x;
    IntList p;

    for (p = this; p != null && L != null; p = p.tail, L = L.tail) {
      if (p.head != L.head) {
        return false;
      }
    }
    if (p != null || L != null) {
      return false;
    }
    return true;
  }

  /** If a cycle exists in the IntList, this method
   *  returns an integer equal to the item number of the location where the
   *  cycle is detected.
   *
   *  If there is no cycle, the number 0 is returned instead. This is a
   *  utility method for lab2. You are not expected to read, understand, or
   *  even use this method. The point of this method is so that if you convert
   *  an IntList into a String and that IntList has a loop, your computer
   *  don't get stuck in an infinite loop.
   */

  private int detectCycles(IntList A) {
    IntList tortoise = A;
    IntList hare = A;

    if (A == null)
      return 0;

    int cnt = 0;


    while (true) {
      cnt++;
      if (hare.tail != null)
        hare = hare.tail.tail;
      else
        return 0;

      tortoise = tortoise.tail;

      if (tortoise == null || hare == null)
        return 0;

      if (hare == tortoise)
        return cnt;
    }
  }

  @Override
  /** Outputs the IntList as a String. You are not expected to read
    * or understand this method. */
  public String toString() {
    Formatter out = new Formatter();
    String sep;
    sep = "(";
    int cycleLocation = detectCycles(this);
    int cnt = 0;

    for (IntList p = this; p != null; p = p.tail) {
      out.format("%s%d", sep, p.head);
      sep = ", ";

      cnt++;
      if ((cnt > cycleLocation) && (cycleLocation > 0)) {
        out.format("... (cycle exists) ...");
        break;
      }
    }
    out.format(")");
    return out.toString();
  }
}

IntlistTest.java
import static org.junit.Assert.*;
import org.junit.Test;

public class IntListTest {

    /** Example test that verifies correctness of the IntList.list static
     *  method. The main point of this is to convince you that
     *  assertEquals knows how to handle IntLists just fine.
     */

    @Test
    public void testList() {
        IntList one = new IntList(1, null);
        IntList twoOne = new IntList(2, one);
        IntList threeTwoOne = new IntList(3, twoOne);

        IntList x = IntList.list(3, 2, 1);
        assertEquals(threeTwoOne, x);
    }

    @Test
    public void testdSquareList() {
      IntList L = IntList.list(1, 2, 3);
      IntList.dSquareList(L);
      assertEquals(IntList.list(1, 4, 9), L);
    }

    /** Do not use the new keyword in your tests. You can create
     *  lists using the handy IntList.list method.  
     *
     *  Make sure to include test cases involving lists of various sizes
     *  on both sides of the operation. That includes the empty list, which
     *  can be instantiated, for example, with
     *  IntList empty = IntList.list().
     *
     *  Keep in mind that dcatenate(A, B) is NOT required to leave A untouched.
     *  Anything can happen to A.
     */

    //TODO:  Create testSquareListRecursive()
    @Test
    public void testSquareListRecursive(){
        IntList L = IntList.list(1,2,3);
        IntList result = IntList.squareListRecursive(L);
        assertEquals(IntList.list(1,4,9),result);
    }
    //TODO:  Create testDcatenate and testCatenate
    @Test
    public void testDcatenate(){
        IntList A = IntList.list(1,2,3);
        IntList B = IntList.list(4,5,6);
        IntList C = IntList.dcatenate(A,B);
        assertEquals(IntList.list(1,2,3,4,5,6),C);
    }

    @Test
    public void testCatenate(){
        IntList A = IntList.list(1,2,3);
        IntList B = IntList.list(4,5,6);
        IntList C = IntList.catenate(A,B);
        assertEquals(IntList.list(1,2,3,4,5,6),C);
        assertEquals(IntList.list(1,2,3),A);
    }

    /* Run the unit tests in this file. */
    public static void main(String... args) {
        jh61b.junit.textui.runClasses(IntListTest.class);
    }      
}   

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

a0106660 发表于 2015-6-13 15:37:45 | 显示全部楼层
a0106660 发表于 2015-6-13 15:33
来交作业了:
IntList.java:
import java.util.Formatter;

全选复制的时候,发现底下还有代码......不过现在可以不用管
回复 支持 反对

使用道具 举报

whosays 发表于 2015-6-14 04:16:02 | 显示全部楼层
为了不折磨视觉,只发填空的代码:
Triangle.java:
    //TODO:  return either "Equilateral", "Isosceles", or "Scalene"
    if ((s1 == s2) && (s2 == s3)) {
      return "Equilateral";
    } else if ((s1 == s2) || (s2 == s3) || (s3 == s1)) {
      return "Isosceles";
    } else {
      return "Scalene";
    }
  }
}

TriangleTest.java:
@Test
    public void testEquilateral() {
      Triangle t = new Triangle(25, 25, 25);
      String result = t.triangleType();
      assertEquals("Equilateral", result);
    }

    @Test
    public void testIsosceles() {
      Triangle t = new Triangle(15, 15, 10);
      String result = t.triangleType();
      assertEquals("Isosceles", result);
    }

    @Test
    public void testNegative() {
      Triangle t = new Triangle(-15, 15, 10);
      String result = t.triangleType();
      assertEquals("At least one length is less than 0!", result);
    }

    @Test
    public void testInvalid() {
      Triangle t = new Triangle(10, 15, 30);
      String result = t.triangleType();
      assertEquals("The lengths of the triangles do not form a valid triangle!", result);
    }

    //TODO: CREATE MORE TESTS

    public static void main(String[] args) {
      //TODO: RUN TESTS
      jh61b.junit.textui.runClasses(TriangleTest.class);
    }

IntList.java
public static IntList dcatenate(IntList A, IntList B) {
    //TODO:  fill in method
    IntList temp = A;
    while (A.tail != null) {
      A = A.tail;
    }
    A.tail = B;

    return temp;
  }

  /** Returns a list consisting of the elements of A followed by the
   ** elements of B.  May NOT modify items of A.  Use 'new'. */
  public static IntList catenate(IntList A, IntList B) {
    //TODO:  fill in method
    // recursively
    if (A == null) {
      return B;
    } else if (B == null) {
      return A;
    }
   
    return new IntList(A.head, catenate(A.tail, B));
  }
IntListTest.java
//TODO:  Create testSquareListRecursive()
    @test
    public void testSquareListRecursive() {
        IntList L = IntList.list(1, 2, 3);
        IntList empty = IntList.list();
        IntList L1 = IntList.squareListRecursive(L);
        IntList empty1 = IntList.squareListRecursive(empty);
        assertEquals(IntList.list(1, 2, 3), L);
        assertEquals(IntList.list(1, 4, 9), L1);
        assertEquals(IntList.list(), empty);
        assertEquals(IntList.list(), empty1);
    }
    //TODO:  Create testDcatenate and testCatenate
    @Test
    public void testDcatenate() {
        IntList a = IntList.list(1, 2, 3);
        IntList b = IntList.list(4, 5);
        IntList.dcatenate(a, b);
        assertEquals(IntList.list(1, 2, 3, 4, 5), a);
        assertEquals(IntList.list(4, 5), b);
    }

    @Test
    public void testCatenate() {
        IntList a = IntList.list(1, 2, 3);
        IntList b = IntList.list(4, 5);
        IntList c = IntList.catenate(a, b);
        assertEquals(IntList.list(1, 2, 3, 4, 5), c);
        assertEquals(IntList.list(1, 2, 3), a);
        assertEquals(IntList.list(4, 5), b);
    }

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

HNAKXR 发表于 2016-1-29 16:07:33 | 显示全部楼层
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-10 21:21

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表