標籤

日記 (44) 屬靈 (6) 心情 (4) 教會 (4) 減肥 (4) 桌上遊戲 (3) 轉錄 (3) 音樂 (3) 讀書心得 (2) 假期 (1) 回憶 (1) 工作 (1) 戰報 (1) 星座 (1) 程式語言 (1) 閒聊 (1) 食記 (1)

2012年5月23日 星期三

重構與測試 LV1


假設有個程式如下

public class Main {
  private double _primaryForce=0.2, _secondaryForce=0.1, _mass=2;
int _delay=2;

  public static void main(String[] args) {

Main a= new Main();
System.out.println(a.getDistanceTravelled_v1(0));
  }

  public Main() {
super();
  }


  public Main(double _primaryForce, double _secondaryForce, double _mass,
int _delay) {
super();
this._primaryForce = _primaryForce;
this._secondaryForce = _secondaryForce;
this._mass = _mass;
this._delay = _delay;
}


  double getDistanceTravelled_v1(int time) {
double result = 0;
double acc = _primaryForce / _mass;
int primaryTime = Math.min(time, _delay);
result = 0.5 * acc * primaryTime * primaryTime;
int secondaryTime = time - _delay;
if (secondaryTime >; 0) {
double primaryVel = acc * _delay;
result += primaryVel * secondaryTime + 0.5 * acc * secondaryTime
* secondaryTime;
}
return result;
  }

}

我首先要作的事情是,建構測試環境,我使用eclipse以及JUnit來作為測試以及開發工具。
JUnit作的事情是單元測試(Unit Testing),這概念中的最小單元是指函式或方法。當我們使用JUnit測試時,我們會不斷嘗試用使函式崩壞的參數作測試,這絕對不是犯賤,相信我。
單元測試相關閱讀





沒有留言: