锁定老帖子 主题:TDD练习
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-06-03
今天在收集中看到一道面试题,网络引用是:http://www.iteye.com/topic/954262。题是这样的: 如何编程实现:输入M,N两个数,从1至N开始循环数数,每数到M输出该数值,直至全部输出。例如M是3,N是20,那么相当有20个人,1,2,3循环报数,数到3的就打印出他的序号,直到所有这20个数全部输出为止。 题目摘自:http://jerval.iteye.com/blog/1068563 尝试使用TDD编写产品代码,在实现时发现测试覆盖率不够;有哪位大虾能说说测试粒度该怎样规划? 测试代码:
代码:
public class Deliver { public static final int FIRST_PLAYER = 0; public static final int FIRST_NUMBER = 1; private int[] evicted; private int evictPos; private int repeat; private int[] players; private int current; public Deliver(int count) { evicted = new int[count]; } private void reInit(int repeat) { evictPos = 0; this.repeat = repeat; players = createPlayers(); current = FIRST_PLAYER; } private int[] createPlayers() { int count = evicted.length; int[] players = new int[count]; for (int k = 0; k < count; k++) players[k] = k + FIRST_NUMBER; return players; } public int[] play(int repeat) { reInit(repeat); replay(); return evicted; } private void replay() { talk(FIRST_NUMBER); } private void talk(int numberOff) { if (shouldEvict(numberOff)) { evict(); return; } deliver(numberOff); } private boolean shouldEvict(int numberOff) { return numberOff >= repeat; } private void evict() { evicted[evictPos++] = players[current]; if (isOnePlayerOnly()) return; players = remove(current); replay(); } private boolean isOnePlayerOnly() { return players.length == 1; } private int[] remove(int index) { int[] remaining = new int[players.length - 1]; int next = index + 1; System.arraycopy(players, FIRST_PLAYER, remaining, FIRST_PLAYER, index); System.arraycopy(players, next, remaining, index, players.length - next); return remaining; } private void deliver(int numberOff) { current = nextPlayer(); talk(numberOff + 1); } private int nextPlayer() { int next = current + 1; return next >= players.length ? FIRST_PLAYER : next; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-06-03
最后修改:2011-06-03
引用 @Test public void manyPlayers() throws Exception { Deliver deliver=new Deliver(20); System.out.println(Arrays.toString(deliver.play(3))); } 这个叫测试么? |
|
返回顶楼 | |
发表时间:2011-06-03
人肉测试。。。。
|
|
返回顶楼 | |
发表时间:2011-06-03
抛出异常的爱 写道 引用 @Test public void manyPlayers() throws Exception { Deliver deliver=new Deliver(20); System.out.println(Arrays.toString(deliver.play(3))); } 这个叫测试么? 这不是测试,这是这个问题的答案~!虽然可读性比较强,但我觉得这个测试是有问题的,测试粒度太大;我应该如何着手编写粒度适中的测试,希望大家给出各自的意见~! |
|
返回顶楼 | |
浏览 2751 次