我可以使用assert来强制执行类型定义.假设有一个变量,double d你如何assert断言d是双精度?如果assert不适用(我打赌不是),还有其他选择吗?我特别希望测试调试过程中隐含的类型转换,而从功能中受益assert和#define NDEBUG.
PS显然我想将它用于任何类型定义,这里只使用double作为示例.该解决方案应该是跨平台兼容的并且与C++ 03兼容.
我想将错误检查添加到我的类设置器中.例如,假设有一个类MyClass,它带有一个私有成员变量x:
void MyClass::setX(double input)
{
// assert x is double
x = input;
}
Run Code Online (Sandbox Code Playgroud) 使用assert比较浮动列表似乎直接开箱即用 - 有没有人知道幕后发生了什么,例如EPSILON的价值是什么?
作为一名C程序员,这一切都让我感到不安......我原本以为断言可能只是比较指针,但似乎做了一些明智的事情:
a = [1.0,2.0]
b = [1.0,2.0]
c = [1.0,2.01]
d = [1.0, 2.0000000000000001]
assert a==b # ok
assert a==c # no go
assert a==d # ok
Run Code Online (Sandbox Code Playgroud) 是否有可能在调用中使用lambda assert()?
当我尝试以下...
assert([&]() -> bool{
sockaddr_storage addr; int addrlen = sizeof(addr);
return (getsockname(this->m_Socket, (sockaddr*)&addr, &addrlen) != 0) ? false : true;
});
Run Code Online (Sandbox Code Playgroud)
......我收到了错误
错误C2675:一元'!' :'`anonymous-namespace'::'没有定义此运算符或转换为预定义运算符可接受的类型
我做了一个小测试,在第二个测试中我得到一个断言错误(0 而不是 1):
package tests;
import static org.junit.Assert.*;
import org.junit.Test;
import javax.swing.*;
public class MenuTest {
@Test
public void testElementsAddition() {
JMenuItem mItem1 = new JMenuItem();
JMenuItem mItem2 = new JMenuItem();
JMenu menu = new JMenu();
mItem1.add(mItem2);
assertEquals(1, mItem1.getComponentCount());
menu.add(mItem1);
assertEquals(1, menu.getComponentCount());
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法为什么会发生这种情况?
我想为实现IDisposable的类编写单元测试.该类有许多私有字段,也实现IDisposable.在我的测试中,我想验证当我调用时Dispose(),它正确调用Dispose()其所有IDisposable字段.基本上,我希望我的单元测试看起来像这样:
var o = new ObjectUnderTest();
o.Dispose();
Assert.IsFalse(ObjectHasUndisposedDisposables(o));
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用反射来实现这一目标.看起来这是一个相当普遍的要求,但我找不到它的任何例子.
有人试过吗?
编辑 - 我不想将Disposables注入被测试的类中.
我必须将MS单元测试转换为NUnit并遇到此断言。
Assert.AreEqual(collection.Select(item => item.location.id).Distinct().Count(), 1);
Run Code Online (Sandbox Code Playgroud)
我希望会有一种优雅的方式来编写带有约束的内容,但我一直找不到。我的解决方案是这样,但是我对此并不满意:
Expect(collection.Select(item => item.location.id).Distinct().Count(), Is.EqualTo(1));
Run Code Online (Sandbox Code Playgroud)
在意图更易读的地方,有没有更好的方式写断言?(使用Has.或Map(collection).)
编辑2:
我刚刚意识到明确说明意图可能会有所帮助:
集合中的所有项目都具有相同的位置ID(不知道该ID是什么)
编辑1:
这是集合的JSON外观:
[{itemId=1, location={name="A", id=1}},
{itemId=2, location={name="A", id=1}},
{itemId=3, location={name="A", id=1}}]
Run Code Online (Sandbox Code Playgroud)
unique.count = 1 =>通过
[{itemId=1, location={name="A", id=1}},
{itemId=2, location={name="A", id=1}},
{itemId=4, location={name="B", id=2}}]
Run Code Online (Sandbox Code Playgroud)
unique.count = 2 =>失败
编辑3:根据Fabio的答案,我的最终解决方案
IEnumerable<long?> locationIds = collection.Select(item => item.location.id);
Expect(locationIds, Has.All.EqualTo(locationIds.FirstOrDefault()));
Run Code Online (Sandbox Code Playgroud) 我写了一个小程序,应检查给定列表是否是来自另一个列表的子列表,并返回True或False:
def is_sublist_of(sublist, given):
""" Returns whether the sublist is part of the given combination.
The order of the sublist must also correspond to the order of the
corresponding part in the given combination."""
return sublist in [given[i:i+len(sublist)] for i in range(0,len(given)-len(sublist))]
Run Code Online (Sandbox Code Playgroud)
此代码是我必须执行的赋值的一部分,但给定的断言之一是:
simple_list = [1, 2, 3, 4]
for element in simple_list:
assert is_sublist_of([element], simple_list)
assert not is_sublist_of([5], simple_list)
Run Code Online (Sandbox Code Playgroud)
我的程序未通过此测试.这是否意味着我的程序在某些特殊情况下不起作用?感谢您对此进行调查.
在某些情况下,我使用Doyren库(libtcod)在Python中制作了一个Roguelike游戏。我更习惯于对对象进行强类型化的C ++。
我正在编写几个类,例如GameMap,GameObject等。其中许多类都包含需要某些类型的方法,例如:
class GameMap:
...
def add_object(self, game_object, x, y):
...
Run Code Online (Sandbox Code Playgroud)
此方法将GameObject game_object添加到地图上的坐标(x,y)。显然有几种方法可以滥用此功能:
我的问题是:处理方法滥用的Python方法是什么?
我看到几种可能性:
选项1:在方法开始时布置一系列断言:
def add_object(self, game_object, x, y):
assert(isinstance(game_object, GameObject)
assert(type(x) == type(y) == int)
assert(0 <= x < self.map_width and 0 <= y < self.map_height)
...
Run Code Online (Sandbox Code Playgroud)
当我将它们复制并粘贴到GameMap中的许多方法中时,这些断言变得相当重复,这就是为什么我还提供选项2的原因:
选项2:在自己的函数中编写断言,并在需要时调用它们以防止复制+粘贴
def check_game_object(self, game_object):
assert(isinstance(game_object, GameObject)
def check_coordinate(self, x, y):
assert(type(x) == type(y) == int)
assert(0 <= x < self.map_width and 0 <= y < self.map_height)
def add_object(self, game_object, x, …Run Code Online (Sandbox Code Playgroud) 有人可以解释何时使用,什么是最佳的断言适用场景?
我的观点是:
if not then raise-O,它将被忽略那么,源代码(而非单元测试)中的使用场景是assert什么?
从我的传统经验来看,assert应该只存在于单元测试中,实际上并不能理解为什么它开始越来越多地出现在Python项目代码中。
所以问题是:我应该使用什么JUnit命令来检查预期不等于实际值.例如,我使用这样的assertEquals
assertEquals(tr1.detectTriangle(), tr1.TR_EQUILATERAL);
Run Code Online (Sandbox Code Playgroud)
所以这里预期变量是2但实际是1并且测试失败.我应该用什么命令来通过这个测试?
assert ×10
python ×4
c# ×2
c++ ×2
java ×2
junit ×2
lambda ×2
unit-testing ×2
casting ×1
collections ×1
debugging ×1
dispose ×1
list ×1
nunit ×1
python-3.x ×1
reflection ×1
sublist ×1
swing ×1
testing ×1