我有一个没有内存的功能.
我想在调用此函数后断言该函数释放了给定的内存.
我无法改变这个功能中的任何东西.
对于单一测试我需要它.我想测试我的函数,我想检查一下我的函数在调用之后真的释放了内存
问题出在代码中
void func(char *mem)
{
// Some where in the function there is a free of the memory
// The free could be into if condition so there is a risk to not be executed
}
int main()
{
char *mem = malloc(20);
func(mem);
// how to assert here that the memory is freed?
}
Run Code Online (Sandbox Code Playgroud) 编辑:正如人们在下面指出的那样,问题与断言有关。谢谢您的帮助!
我有一个枚举集,我试图将其等同,但由于某种原因,它无法正常工作。其声明如下:
typedef NS_ENUM(NSUInteger, ExUnitTypes) {
kuNilWorkUnit,
kuDistanceInMeters,
//end
kuUndefined
};
Run Code Online (Sandbox Code Playgroud)
我在这里使用它:
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
if (exUnit == kuNilWorkUnit)
{
assert("error with units");
}
///.... more stuff
}
Run Code Online (Sandbox Code Playgroud)
Xcode不会触发我的断言。编辑:断言仅用于测试。我也用过NSLog。即使该值显然是kuNilWorkUnit,该条件的取值也不是正确的。

有人对我做错事情有任何建议或想法吗?
我正在为课程分配工作,我认为我让程序正常工作,但现在我想对它进行一些修改,以便更好地理解断言.代码如下 -
#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;
// Sample program that shows how command line arg works, in Unix g++
// Note argc and argv
// Also shows use of system call, that can launch any program
// system launches 'ls' to display files in the dir
void runAssert(int);
int main(int argc, char *argv[])
{
cout << "Number of inputs: " << argc << endl;
cout << "1st argument: " << argv[0] << endl;
system ("ls"); …Run Code Online (Sandbox Code Playgroud) 我注意到一些代码通常看起来像这样:
#ifdef DEBUG
assert(i == 1);
#endif //DEBUG
Run Code Online (Sandbox Code Playgroud)
并且您可能在原始代码中有几个这样的块.必须写出每个块是乏味和混乱的.
拥有这样的功能是否合理:
auto debug_assert = [](auto expr) {
#ifdef DEBUG
assert(expr);
#endif //DEBUG
};
Run Code Online (Sandbox Code Playgroud)
或类似的东西:
#ifdef DEBUG
auto debug_assert = [](bool expr) {
assert(expr);
};
#else //DEBUG
void debug_assert(bool expr) {}
#endif //DEBUG
Run Code Online (Sandbox Code Playgroud)
在未指定DEBUG标志时获得零成本断言?(即它应该具有相同的效果,就好像它没有运行lambda等没有放入代码中那样,并且由g ++/clang编译器优化).
既然assert简单地检查其参数中的语句是否为真,为什么我们不简单地使用if条件来检查呢?
断言的用法
void print_number(int* somePtr) {
assert (somePtr !=NULL);
printf ("%d\n",*somePtr);
}
Run Code Online (Sandbox Code Playgroud)
if 的用法
void print_number(int* somePtr) {
if (somePtr != NULL)
printf ("%d\n",*somePtr);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出两者之间的区别以及使用其中一种的优势吗?
另外,为什么大多数人喜欢assert在if这里?
有谁知道VS2017是否已经支持代码合同,如此处所见的C++ 17代码合同?
当我尝试使用它们时
explicit IniHandler(std::string fileName) [[expects: fileName != nullptr]]
{
this->fileName = fileName;
}
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用.
我正在使用命令行选项/std:c++latest但仍然收到警告"标识符已被删除".
任何帮助很高兴:)
我正在尝试在 python 中实现一个 Vector3 类。如果我用 c++ 或 c# 编写 Vector3 类,我会将 X、Y 和 Z 成员存储为浮点数,但在 python 中我读到ducktyping 是要走的路。所以根据我的 c++/c# 知识,我写了这样的东西:
class Vector3:
def __init__(self, x=0.0, y=0.0, z=0.0):
assert (isinstance(x, float) or isinstance(x, int)) and (isinstance(y, float) or isinstance(y, int)) and \
(isinstance(z, float) or isinstance(z, int))
self.x = float(x)
self.y = float(y)
self.z = float(z)
Run Code Online (Sandbox Code Playgroud)
问题是关于断言语句:在这种情况下你会使用它们还是不使用它们(数学的 Vector3 实现)。我也用它来做类似的操作
def __add__(self, other):
assert isinstance(other, Vector3)
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
Run Code Online (Sandbox Code Playgroud)
你会在这些情况下使用断言吗?根据这个网站:https : //wiki.python.org/moin/UsingAssertionsEffectively它不应该被过度使用,但对于我这个一直使用静态类型的人来说,不检查相同的数据类型是非常奇怪的。
我有一个方法.此方法可能返回Future.failed(.....)或Future.successful(()).
def calculate(x:Int,y:Int):Future [Unit] = {........}
现在我需要测试这个方法.断言验证Future.successful(())案例的测试的最佳方法是什么 .
如何assertJsonCount使用带索引的嵌套数组进行调用?
在我的测试中,返回以下 JSON:
[[{"sku":"P09250"},{"sku":"P03293"}]]
Run Code Online (Sandbox Code Playgroud)
但是尝试使用会assertJsonCount返回以下错误:
$response->assertJsonCount(2);
// Failed asserting that actual size 1 matches expected size 2.
Run Code Online (Sandbox Code Playgroud) 假设我有一个assert包含大量计算错误消息的语句(例如,进行多个网络或数据库调用)。
assert x == 5, f"Some computationally heavy message here: {requests.get('xxx')}"
Run Code Online (Sandbox Code Playgroud)
我还可以使用 if 语句编写此代码:
if x != 5:
raise AssertionError(f"Some computationally heavy message here: {requests.get('xxx')}")
Run Code Online (Sandbox Code Playgroud)
我知道后一个选项只会评估错误消息,如果x != 5. 前一种选择呢?我会这么认为,但我不确定。
assert ×10
c++ ×5
python ×2
testing ×2
assertion ×1
c ×1
c++17 ×1
contract ×1
duck-typing ×1
free ×1
future ×1
if-statement ×1
isinstance ×1
json ×1
laravel-5 ×1
memory-leaks ×1
objective-c ×1
phpunit ×1
scala ×1
scalatest ×1