一般情况下鼓励使用初始化列表.现在让我们说我有以下代码(简单的例子只是为了让问题更清晰):
class foo
{
public:
foo(ptr1* a, ptr2* b) : m_a(a), m_b(b), m_val(a->val) {}
/* code and members here */
};
Run Code Online (Sandbox Code Playgroud)
我想a在尝试取消引用它之前检查它是否为NULL val.有什么方法可以在那里进行健全检查吗?
我在多线程c应用程序中发现了这一点.作者评论说它用于在自定义断言函数中使线程崩溃.海湾合作委员会对它很好,但是clang发出以下警告:
note: consider using __builtin_trap() or qualifying pointer with 'volatile'
Run Code Online (Sandbox Code Playgroud)
并且对于assert函数的每次使用,还会发出其中一个:
warning: indirection of non-volatile null pointer will be deleted, not trap
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?__builtin_trap特定于clang?我应该用吗?
我想要做的是像JUnit中的以下内容:
assertTrue(logger.error("the condition is not true"), <a boolean condition>);
Run Code Online (Sandbox Code Playgroud)
所以记录器记录错误消息,记录器可以是例如commons或log4j.
但Junit断言不接受记录器参数,所以有没有办法实现这一点,或者我是否需要尝试捕获断言并在catch块中记录错误消息?
为什么assert宏只在调试配置中做一些有用的事情是一种常见的做法?如果它存在以测试不变量并检测编码错误,那么继续在生产软件中进行同样的大繁荣会不会更容易?
我有一些S60的背景和存在__ASSERT_ALWAYS和__ASSERT_DEBUG,后者相当于assert.
我的代码广泛使用这样的编译器断言来在构建时标记错误而不是运行时,并通过在运行时不执行断言来提高性能.
#define COMPILER_ASSERT(EXPR) switch (0) {case 0: case (EXPR):;}
Run Code Online (Sandbox Code Playgroud)
都好.我想扩展这个以使用编译器断言来实现以下情况.假设我有一个从100个地方调用的宏,其中99个传递一个固定值,其中1个传递一个变量.我如何编写宏以使其成为99个位置的编译器断言和最后一个中的运行时断言.
如果我可以保证MY_FUNCTION()总是以固定值调用,我可以这样编码.
void my_function(int x)
{
//do something
}
#define MY_FUNCTION(X) \
COMPILER_ASSERT(X != 0); \
my_function(X)
//These can all take advantage of a compiler assert.
MY_FUNCTION(1);
MY_FUNCTION(SOME_HASH_DEFINE);
MY_FUNCTION(sizeof(SOME_STRUCTURE));
//This can't (this is a contrived example - actual code is complex).
int some_variable = 1;
MY_FUNCTION(some_variable);
Run Code Online (Sandbox Code Playgroud)
所以,如果我不能保证X是固定的,但想要利用每次调用MY_FUNCTION()的位置,我该怎么编码呢?就像是:
#define MY_FUNCTION(X) \
if (X is a fixed value) COMPILER_ASSERT(X != 0); \
else assert(X != 0); \
my_function(X)
Run Code Online (Sandbox Code Playgroud)
将调用MY_FUNCTION()重新编码为仅传递固定值对我来说不是一个选项.是的我可以定义MY_FUNCTION_FIXED_X和MY_FUNCTION_VARIABLE_X,但是这会将所有这些暴露给调用代码.
谢谢你的帮助.NickB
我的一个脚本有一些问题...我会把问题放在子弹中.
MATCH2,但忽略例如在过去的类数据的括号数据MATCH2有Mdata (D)需要的匹配MDataMATCH1我可以设置,MATCH1以便它只使用MATCH1[-1:]哪个最终会给我这个例子中的数字.Testing.pyimport re
import pandas
from pandas.util.testing import assert_frame_equal
# each block in the text file will be one element of this list
matchers = [[]]
i = 0
with open('testing.txt') as infile:
for line in infile:
line = line.strip()
# Blocks are seperated by blank lines
if len(line) == 0:
i += …Run Code Online (Sandbox Code Playgroud) 我可以转到默认Visual Studio 2010项目的设置,并在"配置属性">>"C/C++">>"预处理器"的"预处理器定义"中查看NDEBUG或_DEBUG定义:

如果我进入默认Visual Studio 2015项目的设置"预处理器定义"为空!

我甚至没有在Visual Studio 2015 .vcxproj中看到NDEBUG或_DEBUG提到过.我知道它是正确定义的,因为assert(false)只在Debug配置中触发.微软在Visual Studio 2015中将这些定义放在哪里?我可以修改它们,还是从我那里取走它?
编辑:
尼尔的答案是正确的.我不明白的是,assert如果在Release配置中触发,则会发生不同的情况.此程序不会弹出assert对话框,但它不会执行以下system("pause")任一操作:
void main() {
assert(false);
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
相反,当我在Release配置从Visual Studio我看到控制台窗口弹出,并立即关闭运行它,所以我认为在assert不火的原因,我没有看到一个assert对话框.但是,如果我从终端运行已编译的Release可执行文件,它会立即结束,但它会输出:
断言失败:false,文件temp.cpp,第9行
因此,断言是在发布配置射击,它只是不弹出一个assert对话框.这意味着NDEBUG事实上没有被定义.
c++ assert visual-studio-2010 visual-studio visual-studio-2015
所以我试图测试一个函数,它是一个客户端函数(un-finish),这就是它嵌入测试本身的原因(直到我能找到更好的解决方案).
我遇到的问题是当我测试函数是否抛出TypeError时.
我理解问题是因为它正在测试返回值而不是函数本身,我不确定如何解决这个问题.
任何和所有帮助表示赞赏!
var test = require('tape');
test('GenerateRandomNumber Tests', function(assert){
/**
* Generates a random number between the min/max
* @param {int} the min value
* @param {int} the max value
* @param {array} list of values already stored
* @return {mixed} int if success, false if exception thrown
**/
var GenerateRandomNumber = function( min, max, tickets ){
try{
if(!tickets instanceof Array){
throw new TypeError();
}
min = (min) || 0;
max = (max) || 200;
var …Run Code Online (Sandbox Code Playgroud) 如果我想编写自己的test.cpp来检查另一个.cpp文件是否正在输出我希望它输出的方式,那么无论如何都没有明确打印它吗?
换句话说,有什么比如
assert(output_of_file_being_tested, "this is the correct output");
Run Code Online (Sandbox Code Playgroud)
其中output_of_file_being_tested应该是"cout"ed.
是否可以测试许多情况,例如:
[TestCase(""), TestCase(null), TestCase("String"), TestCase("2010-07-14T00:00:00.000Z"), TestCase("201-07-14T00:00:00.000Z")]
Run Code Online (Sandbox Code Playgroud)
当我从带有反序列化json的API获得响应到我的Error对象时,我认为响应主体消息和HttpStatusCode在API的不同VALIDATIONS之后是正确的,并且从我的断言中它是正确的.
我从Json反序列化为泛型类型的方法:
public TType GetResponseJsonAsObject<TType>(HttpResponseMessage content)
{
var contentResult = content.Content.ReadAsStringAsync().Result;
var specialCase = JsonConvert.DeserializeObject<TType>(contentResult);
return specialCase;
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试方法:
[TestCase(""), TestCase(null), TestCase("String"), TestCase("2010-07-14T00:00:00.000Z"), TestCase("201-07-14T00:00:00.000Z")]
public void PostAPI_SpecialCaseRequestDate_WithError_ProceedsSuccessfully(string date)
{
// arrange
SpecialCaseRequest data = new SpecialCaseRequest()
{
Name = "Test",
ExpirationDateTime = date,
};
// act + assert
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
PostMethods sendJsonDemo = new PostMethods();
var response = sendJsonDemo.SendJsonDemo(json);
var responseBody = sendJsonDemo.GetResponseJsonAsObject<Error>(response);
Assert.IsNotNull(responseBody);
Assert.AreEqual("Date parameter cannot be empty string", responseBody.Message); …Run Code Online (Sandbox Code Playgroud) assert ×10
c++ ×4
c ×3
unit-testing ×2
c# ×1
clang ×1
cout ×1
debugging ×1
gcc-warning ×1
java ×1
javascript ×1
json ×1
junit ×1
logging ×1
node.js ×1
node.js-tape ×1
output ×1
pandas ×1
python ×1
python-2.7 ×1
regex ×1
testing ×1
volatile ×1