assert

What Standard Calls are Actually Macros

不打扰是莪最后的温柔 提交于 2019-12-30 12:25:24
问题 I asked a question here about assert which is implemented in the standard as a macro, not a function. This had caused me an issue because the way that assert appears to be a function in the way it takes a parameter: assert(true) Thus I tried to use it as: std::assert(true) and of course being a macro that didn't work. My question is this: Are there any other macros provided by the standard library which would appear as functions that take parameters? 回答1: If we look at [headers] paragraphs 5

Assert to compare two lists of objects C#

柔情痞子 提交于 2019-12-30 11:22:14
问题 I am currently trying to learn how to use unit testing, and I have created the actual list of 3 animal objects and the expected list of 3 animal objects. The question is how do I Assert to check the lists are equal? I have tried CollectionAssert.AreEqual and Assert.AreEqual but to no avail. Any help would be appreciated. The test method: [TestMethod] public void createAnimalsTest2() { animalHandler animalHandler = new animalHandler(); // arrange List<Animal> expected = new List<Animal>();

Is it legal to use side-effects in exceptions thrown by constexpr?

心不动则不痛 提交于 2019-12-30 07:58:10
问题 Normally, constexpr must be free of side-effects. However, I just discovered that it is possible to use side-effects in the constructors of thrown exceptions. That technique can be used to emulate assert() for constexpr functions, as it is demonstrated in the following program. #include <iostream> #include <cstdlib> #include <stdexcept> struct constexpr_precond_violated : std::logic_error { constexpr_precond_violated(const char* msg) : std::logic_error(msg) { std::cerr << msg << '\n'; abort()

【dart学习】-- Dart之类和对象

你说的曾经没有我的故事 提交于 2019-12-30 05:38:38
一,概述    类 (Class)是面向对象程序设计,实现信息封装的基础。类是一种用户定义的类型。每个类包含数据说明和一组操作数据或传递消息的函数。类的实例称为对象。 Dart的类与其它语言都有很大的区别,比如在dart的类中可以有无数个构造函数,可以重写类中的操作符,有默认的构造函数,由于dart没有接口,所以dart的类也是接口,因此你可以将类作为接口来重新实现。   Dart是一门使用类和单继承的面向对象语言所有的对象都是类的实例,并且所有的类都是 Object 的子类。 二,类定义 类的定义用 class 关键字 如果未显式定义构造函数,会默认一个空的构造函数 类首字母必须大写 使用 new 关键字和 构造函数 来创建对象 class Person { //未定义父类的时候,默认继承自Object num x; num y; num z; } void main(List<String> args){ var person = new Person();//调用默认的构造函数 person.x = 10; //使用点(.)引用实例变量或方法 person.y = 11; person?.z = 12; //如果p不为空,设置它的变量y的值为4 print(person.x); print(person.y); print(person.z); } 结果: 10 11 12

实现strcat函数,assert宏(断言宏)使用介绍

可紊 提交于 2019-12-30 04:55:24
strcat函数的实现代码如下: char* MyStrcat(char *dst, const char *src) { assert(dst != NULL && src != NULL); char *temp = dst; while (*temp != '\0') temp++; while ((*temp++ = *src++) != '\0'); return dst; } 上述代码为strcat函数的源码,这里assert函数,其实是一个宏的功能可以通过man assert命令来了解,需要assert.h头文件,这里最好不要用strlen()函数去获得src字符串的长度,因为strlen包含在string.h头文件,这个头文件已经包含了strcat函数了,再说我们要实现功能一般是不用其他已有的函数的(特制一些函数是用头文件包含起来的,当然想stdio.h之类的除外), assert()宏用法: 断言捕捉不应该发生的非法情况 if(假设成立) { 程序正常运行; } else { 报错&&终止程序!(避免由程序运行引起更大的错误) } 功能:在运行过程中,如果assert 的参数为假,那么程序就会中止(一般地还会出现提示对话,说明在什么地方引发了assert)。 注意要点可以查看: https://www.cnblogs.com/thisway/p/5558914

伪造,嘲笑和存根之间有什么区别?

被刻印的时光 ゝ 提交于 2019-12-29 21:31:20
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我知道我如何使用这些术语,但是我想知道是否存在接受 伪造 , 模拟 和 存根 的单元测试定义? 您如何为测试定义这些? 描述您可能会使用每种情况的情况。 这是我的用法: Fake :实现接口但包含固定数据且没有逻辑的类。 只需根据实现返回“好”或“坏”数据。 Mock :一个实现接口并允许动态设置要返回的值/从特定方法抛出异常的类,并提供检查是否已调用/未调用特定方法的能力。 存根(Stub) :类似于模拟类,不同之处在于它不提供验证方法是否已被调用的能力。 模拟和存根可以手动生成,也可以由模拟框架生成。 伪类是手工生成的。 我主要使用模拟来验证我的类和依赖类之间的交互。 一旦验证了交互作用并测试了代码中的替代路径,便会使用存根。 我主要使用伪造的类来抽象出数据依赖性,或者当模拟/存根过于繁琐而无法每次设置时。 #1楼 让我感到惊讶的是,这个问题已经存在了很长时间,而且还没有人根据 Roy Osherove的“单元测试的艺术” 给出答案。 在“ 3.1介绍存根”中,将存根定义为: 存根是系统中现有依赖项(或协作者)的可控替代。 通过使用存根,您可以测试代码而无需直接处理依赖项。 并将存根和模拟之间的区别定义为: 关于模拟与存根之间要记住的主要事情是,模拟就像存根一样,但是您针对模拟对象断言

assert() with message

浪尽此生 提交于 2019-12-29 13:43:46
问题 I saw somewhere assert used with a message in the following way: assert(("message", condition)); This seems to work great, except that gcc throws the following warning: warning: left-hand operand of comma expression has no effect How can I stop the warning? 回答1: Use -Wno-unused-value to stop the warning; (the option -Wall includes -Wunused-value ). I think even better is to use another method, like assert(condition && "message"); 回答2: Try: #define assert__(x) for ( ; !(x) ; assert(x) ) use as

实验2实验报告

老子叫甜甜 提交于 2019-12-29 10:58:30
我将使用一个例子来展示不同的测试用例:一个计算器。该示例计算器很简单,效率并不高,甚至还有一些错误;它仅仅操作整数,并且把结果存储在一个静态变量中。Substract方法并不返回一个有效的结果,而且也没有实现乘法运算,而且看上去在squareRoot方法中还存在一个错误:无限循环。这些错误将帮助说明使用JUnit 4进行测试的有效性。你可以打开和关闭这个计算器,而且你可以清除这些结果。下面是其实现代码: package calc; public class Calculator {   private static int result; // 存储结果的静态变量   public void add(int n) { result = result + n;   }   public void substract(int n) { result = result - 1; // 错误:应该是 "result = result - n"   }   public void multiply(int n) {} // 还没实现   public void divide(int n) { result = result / n;   }   public void square(int n) { result = n * n;   }   public void squareRoot

Best practice for debug Asserts during Unit testing

前提是你 提交于 2019-12-29 10:16:10
问题 Does heavy use of unit tests discourage the use of debug asserts? It seems like a debug assert firing in the code under test implies the unit test shouldn't exist or the debug assert shouldn't exist. "There can be only one" seems like a reasonable principle. Is this the common practice? Or do you disable your debug asserts when unit testing, so they can be around for integration testing? Edit: I updated 'Assert' to debug assert to distinguish an assert in the code under test from the lines in

g++ doesn't compile constexpr function with assert in it

狂风中的少年 提交于 2019-12-29 04:25:44
问题 template<typename T> constexpr inline T getClamped(const T& mValue, const T& mMin, const T& mMax) { assert(mMin < mMax); // remove this line to successfully compile return mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue); } error : body of constexpr function 'constexpr T getClamped(const T&, const T&, const T&) [with T = long unsigned int]' not a return-statement Using g++ 4.8.1 . clang++ 3.4 doesn't complain. Who is right here? Any way I can make g++ compile the code without using