我已经解决了一个 topCoder 问题,当我自己运行它们时,所有测试都通过了。尽管如此,当我运行整个测试类时,其中一些失败了。你能帮我找出这种行为的原因吗?这是我的类(class)和测试:
package com.topcoder.div2.stage1;
import java.util.Arrays;
public class GameOfStones {
private int iterations = 0;
public int count(int[] stones){
int result = checkEquality(stones);
return result;
}
private int checkEquality(int[] stones){
int count = 0;
int sum = 0;
for(int k = 0; k< stones.length;k++){
sum += stones[k];
}
if(stones.length > 0) {
for (int i = 0; i < sum; i++) {
Arrays.sort(stones);
if(stones[stones.length-1] != 3) {
int j = 0;
while (j < stones.length - 1) {
if (stones[j] == stones[j + 1]) {
count++;
}
j++;
}
if (count == stones.length - 1) {
return iterations;
}
stones[0] = stones[0] + 2;
stones[stones.length - 1] = stones[stones.length - 1] - 2;
iterations++;
count = 0;
}
}
}
return -1;
}
}
测试:
package com.topcoder.div2.stage1;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class GameOfStonesTest {
private GameOfStones gameOfStones = new GameOfStones();
@Test
public void test1() {
int expected = 0;
int[] given = {17};
int actual = gameOfStones.count(given);
assertEquals(expected, actual);
}
@Test
public void test2() {
int expected = 3;
int[] given ={7, 15, 9, 5};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
@Test
public void test3() {
int expected = -1;
int[] given ={2, 8, 4};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
@Test
public void test4() {
int expected = -1;
int[] given ={10, 15, 20, 12, 1, 20};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
@Test
public void test5(){
int expected = 277;
int[] given ={17, 1, 27, 29, 13, 1, 27, 3, 19, 3, 25, 1, 11, 9, 7, 17, 31, 25, 5, 11, 31, 9,
15, 3, 3, 3, 11, 11, 1, 41, 5, 95, 7, 3, 41, 31, 7, 13, 15, 5, 17, 3, 9, 3, 11,
27, 1, 23, 15, 5, 43, 11, 17, 7, 1, 3, 13, 69, 3, 43, 21, 1, 25, 1, 3, 11, 5, 43,
13, 7, 15, 1, 1, 55, 37, 9, 5, 7, 21, 3, 23, 15, 1, 9, 3, 35, 13, 17, 7, 17, 27, 5,
9, 19, 13, 1, 1, 1, 29};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
@Test
public void test6(){
int expected = 539;
int[] given ={1, 29, 11, 35, 57, 15, 85, 19, 5, 47, 53, 5, 63, 19, 13, 63, 27, 43, 53, 75, 67, 93, 33, 31, 47, 3,
63, 17, 11, 53, 35, 23, 17, 45, 31, 19, 63, 75, 5, 3, 49, 19, 11, 89, 21, 69,
71, 5, 45, 81, 31, 13, 11, 19, 7, 99, 33, 63, 19, 57, 73, 29, 35, 9, 47,
1, 17, 7, 13, 31, 5, 85, 95, 23, 45, 65, 63, 41, 81, 33, 45, 1, 15,
45, 19, 87, 51, 7, 13, 39, 1, 59, 29, 35, 1, 43};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
@Test
public void test7() {
int expected = 0;
int[] given ={100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
@Test
public void test8() {
int expected = 11;
int[] given ={3, 5, 21, 31};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
@Test
public void test9() {
int expected = 13;
int[] given ={44, 6, 46};
int actual = gameOfStones.count(given);
assertEquals(actual, expected);
}
}
P.S 如果您知道任何改进解决方案的建议,我们非常欢迎您将它们包含在您的答案中。
最佳答案
您在所有测试中共享被测类的单个实例。我会删除初始分配并添加:
private GameOfStones gameOfStones; // Don't create an instance here
@BeforeMethod
public void setUp() {
gameOfStones = new GameOfStones();
}
... 将为每个测试使用一个新实例。好的做法是在每次测试后进行清理:
@AfterMethod
public void tearDown() {
gameOfStones = null;
}
在此处给出的示例中,修复导致问题变为方法范围的类范围变量也可以解决问题,但是随着被测软件变得越来越复杂,最好开始进行适当的测试设置和拆除。
关于java - 测试在单独运行时通过,但在整个测试类运行时不通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26561511/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这