我前几天在c ++中编写了一个函数,它发生在我身上,编译器可以做更多的事情来帮助我防范错误.我的代码的基本要素是这样的 -
void method(SomeType* p)
{
assert(p != 0);
p->something();
}
Run Code Online (Sandbox Code Playgroud)
它被称为这样
SomeType p = NULL;
if (SomeCondition)
{
p = some_real_value;
}
method(p);
Run Code Online (Sandbox Code Playgroud)
很明显,p在运行时可能为null,因此在调试版本中对方法的断言失败.我的错.
但是,似乎编译器可能在编译时捕获到这一点并发出警告,说它已检测到它已发现断言可能被违反的可能性.
好的,这是一个简单的案例,编译器根据程序的某些流程分析和跟踪每个点的可能变量范围,发现指针在该点可能是NULL是相当简单的.
我知道确定是否会违反许多断言可能太难了,但即使是很少次编译器能够告诉我,我编写的代码可能会违反断言,这将有助于我程序更安全.
我认为它可以帮助处理数组索引中的一个错误,例如在循环中: -
assert(index >= 0 && index < array_size);
Run Code Online (Sandbox Code Playgroud)
我想在许多情况下编译器可以在编译时证明索引变量可能超出这些边界并在编译时发出警告.
我意识到,对于编译器而言,这可能是一项太多的工作,但也许有一些工具可以执行这种分析?我一直无法找到谷歌的任何东西,但我想知道这种类型是否存在?或者是否太难以做得好或许有用?
我有一个方法接受sha1哈希的std :: vector作为字符串,其长度必须为20个字符.在单行中断言这个前提条件得到尊重将是很好的.
void MyClass::setSha1Sums(const std::vector<std::string>& sha1Sums)
{
assert(magic_oneliner_which_verifies_that_all_strings_are_20_chars_long);
sha1Sums_ = sha1Sums;
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个服务器端项目,该项目应该接受100多个客户端连接.
它是使用boost :: thread的多线程程序.我boost::lock_guard<boost::mutex>用来锁定共享成员数据的一些地方.还有一个BlockingQueue<ConnectionPtr>包含输入连接.执行BlockingQueue:
template <typename DataType>
class BlockingQueue : private boost::noncopyable
{
public:
BlockingQueue()
: nblocked(0), stopped(false)
{
}
~BlockingQueue()
{
Stop(true);
}
void Push(const DataType& item)
{
boost::mutex::scoped_lock lock(mutex);
queue.push(item);
lock.unlock();
cond.notify_one(); // cond.notify_all();
}
bool Empty() const
{
boost::mutex::scoped_lock lock(mutex);
return queue.empty();
}
std::size_t Count() const
{
boost::mutex::scoped_lock lock(mutex);
return queue.size();
}
bool TryPop(DataType& poppedItem)
{
boost::mutex::scoped_lock lock(mutex);
if (queue.empty())
return false;
poppedItem = queue.front();
queue.pop();
return true;
}
DataType WaitPop() …Run Code Online (Sandbox Code Playgroud) 我有一个包含数千个课程的大型项目。今天,我发现对于某些输入,在启用断言的情况下运行时会得出不同的结果。
由于断言本身不会引发AssertionError(我已经在该断点上出现了断点),因此这意味着代码中隐藏的某个地方是断言语句,它具有有害的副作用。问题是我不知道我的100多个断言中的哪个引起了问题。
由于assert不是Java中的方法而是关键字,因此我对如何设置断言(每次调用assert时都会命中)感到困惑。
我发现了可能的解决方法:
尽管如此,我想知道是否有人知道一种断言断言的方法,即使条件评估为真。
我正在尝试Assert.AreEqual在测试中使用我们的提供商验证DTO.
断言失败的是:
预计: DTO.EmployeeDTO
实际: DataProviders.Mappers.EmployeeMapper
我可以用我们的DTO和/或映射器来完成AreEqual工作吗?我应该覆盖EqualsDTO并使用Assert.Equal吗?
这是我们预期的员工DTO进行比较:
EmployeeDTO expected = new EmployeeDTO
{
Category = "OPS",
Code = "EMPL",
Email = "bill.gates@microsoft.com",
JobDescription = "Philanthropist",
FirstName = "Bill",
Bolander = "Gates",
ResourceID = 1234567,
ResourceNumber = "ABCD1234567",
UserGUID = Guid.Parse("0A76A348-B709-9EF0-9E44-419433E7C90D"),
UserName = "billygates"
};
Run Code Online (Sandbox Code Playgroud)
这是来自数据提供者的调用,它应该返回一个EmployeeDTO我们可以与预期的DTO进行比较的函数.
EmployeeDTO actual = controller.GetEmployee("billygates");
Run Code Online (Sandbox Code Playgroud)
这是GetEmployee应该返回员工DTO 的提供者方法:
public EmployeeDTO GetEmployee(string userName)
{
EmployeeDTO toReturn = null;
//Get employee
toReturn = (from employees in …Run Code Online (Sandbox Code Playgroud) 更新3:
这个页面向我展示了如何在三行中完成它.任何人都知道如何在一条线上完成它?
for (int i = 0; i < 5; i++) {
my_value_to_verify = get_my_values(i);
wchar_t message[200]; // Line 1
_swprintf(message, L"Value %d failed", i); // Line 2
Assert::AreEqual(42, my_value_to_verify, message); // Line 3
}
Run Code Online (Sandbox Code Playgroud)
更新2
正如@JaredPar所指出的那样,我所指的文档实际上是用于C++/CLI.在进行额外搜索之后,看起来以下链接包含对我正在使用的函数的引用.
更新1
从{i}中删除括号也会导致编译错误:
Error: no instance of overloaded function ...AreEqual matches the argument list (int, int, const wchar_t[16], int)
Run Code Online (Sandbox Code Playgroud)
原帖
使用Microsoft文档我正在尝试构造一个将打印格式化消息的Assert语句.例如:
for (int i = 0; i < 5; i++) {
my_value_to_verify = get_my_values(i);
Assert::AreEqual(42, my_value_to_verify, L"Value %d failed", …Run Code Online (Sandbox Code Playgroud) 我想在特定情况下检测和报告错误,使用与Debug.Assert()发布版本相同的行为.我怎样才能做到这一点?
我用QUnit库做了一些Javascript单元测试,然后我想测试方法是否抛出RangeError.它以这种方式完成:
QUnit.test("Resolve slide animation from left", function( assert ) {
var myOwnCreator = new MyOwnCreator();
assert.equal(myOwnCreator.resolveAnimationType("slideSide", "show", "left"),
"slide-left-in");
assert.equal(myOwnCreator.resolveAnimationType("slideSide", "hide", "left"),
"slide-left-out");
assert.throws(myOwnCreator.resolveAnimationType("slideSide", "hide"),
new RangeError(),
" If animation type is 'slideSide', you have to provide correct direction"
+ "to identify animation properly, direction cannot be 'undefined'");
});
Run Code Online (Sandbox Code Playgroud)
第一次和第二次测试通过,因为通过函数(,, slide -...")解析的动画类是可以的.但第三次测试死了:
Died on test #3 at file...
Run Code Online (Sandbox Code Playgroud)
因为它抛出RangeError.它可以抛出RangeError,但我不明白如何在单元测试中捕获它,获取,确定"信息.如果我理解QUnit文档:QUnit抛出文档
我做的一切都好:我传递抛出错误的函数,预期错误的实例和预期的消息.我有人会决定帮助我,我会很高兴 - 提前谢谢你.
我有一个Haskell包,我在其中执行一些检查assert,但是当我的包在客户机上构建时,这些通常是 - 因为,我收集,优化 - 变成了无操作.
有没有办法在assert我的Hackage软件包安装到别人的机器上时坚持不懈; 或者也许我可以使用具有相同效果的替代方法?
我的测试没有捕获带有assertRaises的psycopg2.IntegrityError.我正在使用Flask-SQLAlchemy.
def test_insert_cash_flow(self):
cf = CashFlow()
db.session.add(cf)
self.assertRaises(psycopg2.IntegrityError, db.session.commit)
Run Code Online (Sandbox Code Playgroud)
我的CashFlow SQLAlchemy模型有几个nullable = False字段.它说我的测试失败了,并且IntegrityError被打印到屏幕上,但我的assertRaises没有抓住这个.有人怀疑为什么?
assert ×10
c++ ×4
unit-testing ×3
c# ×2
.net ×1
breakpoints ×1
debugging ×1
eclipse ×1
flask ×1
haskell ×1
java ×1
javascript ×1
mstest ×1
mutex ×1
pthreads ×1
python ×1
qunit ×1
sqlalchemy ×1
stl ×1
throws ×1