assert

续写文件

旧时模样 提交于 2020-01-25 17:33:52
1.获取以前文件的大小 u_long GetFileSize(string strFile) {   long retFileLen; //文件长度 FILE* stream; //文件指针 if((stream = fopen(strFile.c_str(), "rb")) == NULL) return 0; fseek(stream, 0, SEEK_END); retFileLen = ftell(stream); fclose(stream); return retFileLen; } 2.打开文件续写文件 char pBuf[BUF_SIZE]; stream = fopen(stream.c_str(), "ab+"); //打开文件 ASSERT(stream); int nErrCode = fseek(stream, ulPos, SEEK_SET); //移动文件指针,ulPos为以前文件的大小 ASSERT(nErrCode == 0); nErrCode = fwrite(pBuf, 1, ulLen, stream); //写文件,uLen为本次写进内容的大小 ASSERT(nErrCode == uLen); fflush(stream); //清除文件缓冲区 fclose(stream); //关闭文件 来源: https://www.cnblogs

顺序表

空扰寡人 提交于 2020-01-25 16:28:03
顺序表是用一段 物理地址连续 的存储单元一次存储数据元素的线性结构,一般情况下采用数组存储 顺序表分为: 静态顺序表(使用地工厂数组存储) 动态顺序表(使用动态开辟的数组存储) 下面实现的是动态顺序表: C语言中没有这种结构,所以把它们封装成一个结构体,里面有不同类型的元素(数组、有效元素个数、数组总容量),便于管理 typedef int DataType ; //给类型取别名 数组元素类型就很好变 typedef struct SeqList { DataType * _array ; //有效元素(动态开辟的数组) int _size ; //有效元素个数 int _capacity ; //数组的总容量(空间总大小) } SeqList , * PSeqList ; //定义了结构体变量(把struct SeqList重命名为SeqList) 对结构体初始化 数组的大小先设置一个初始值,不够了再扩容 void SeqListInit ( PSeqList ps ) { assert ( ps ) ; //先设置一个容量(假设可以存放3个元素),在这个基础上再动态开辟 ps - > _array = ( DataType * ) malloc ( sizeof ( DataType ) * 3 ) ; if ( NULL == ps - > _array ) { return

Python学习笔记(二)——数组与矩阵

自闭症网瘾萝莉.ら 提交于 2020-01-24 17:34:24
一、Array 1.The array structure import ctypes #Array ADT class Array: # Creates an array with size elements. def __init__(self, size): assert size > 0, "Array size must be > 0" self._size = size # Create the array structure using the ctypes module. PyArrayType = ctypes.py_object * size self._elements = PyArrayType() # Initialize each element. self.clear(None) def __len__(self): return self._size #从这里我们也可以看出为什么len()方法的时间复杂度是O(1) # Gets the contents of the index element. def __getitem__(self, index): assert index >= 0 and index < len(self), "Array subscript out of range" return self._elements

JUnit单元测试

六月ゝ 毕业季﹏ 提交于 2020-01-24 17:16:38
简介 JUnit是一个开源的java语言的单元测试框架 专门针对java语言设计, 使用最广泛, JUnit是标准的单元测试架构 java单元测试是最小的功能单元测试代码, 单元测试就是针对单个java方法的测试 目的 确保单个方法正常运行 测试代码可以作为示例代码 可以自动化运行所有测试并获得报告 Maven依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> 项目结构 断言 在 JDK 1.4 之后, Java 中增加了断言的功能,关键字 assert 断言是java的一种语句,它允许对程序提出一个判断(假设)。断言包含一个布尔表达式,在程序运行中它应该是真。假设程序期望得到某个结果,如果没达成则报错。断言用于确保程序的正确性,避免逻辑错误。 与异常处理类似,断言不能代替正常的检验,只是检测内部的一致性和有效性。断言在运行是检验,可以在程序启动时打开或关闭。 PS:不要在正常的业务逻辑中使用断言。对于程序正确性的判断可以在测试环境使用断言,生成环境则可以关闭该功能。 期望之外的结果: import org.junit.Assert; /** * @author wzm * @version 1.0.0 *

What's the reason for this no-op while-loop used for assert macro?

孤街浪徒 提交于 2020-01-24 12:18:28
问题 I'm reviewing a codebase where assert macro is expanded like this in non-debug configurations: #define assert( what ) while( 0 )( ( void )1 ) which I don't quite get. Obviously the goal is to have a no-op. Then why not expand into an empty string? #define assert( what ) What's the reason for this no-op loop? 回答1: Most likely to avoid compiler warnings. Check whether this code provokes a warning about an empty statement: if (foo); If it does, then do you want the same warning in release mode

How to cause a debug break in firebug

佐手、 提交于 2020-01-24 10:00:28
问题 I am trying to make firebug break when an error is detected. Specifically, I have some internal checks in my code, like assertions, that I want firebug to stop on when they fail. I have tried a few different ways and wondered what other people do? Here are the ways I have tried: Put in some invalid code so that if errors out: function assert(value) { if(! value) dbgbreak(); } // Fails because dbgbreak not defined This works somewhat, but does not stop the code in such a way that I can see the

[C++ compile time assertions]: Can we throw a compilation error if some condition is not met?

半腔热情 提交于 2020-01-24 03:23:04
问题 I wrote a function: template<int N> void tryHarder() { for(int i = 0; i < N; i++) { tryOnce(); } } but I only want it to compile if N is in between 0 and 10. Can I do it? How? 回答1: You can do it with static_assert declaration: template<int N> void tryHarder() { static_assert(N >= 0 && N <= 10, "N out of bounds!"); for(int i = 0; i < N; i++) { tryOnce(); } } This feature is only avaliable since C++11. If you're stuck with C++03, take a look at Boost's static assert macro. The whole idea of

Assertive programming with JavaScript

梦想的初衷 提交于 2020-01-22 13:44:27
问题 I know why assertive programming is good, so I want to use it with JavaScript. However, I don't want to show users error boxes, and it's unusual thing. Just ignore it and make them retry could be better. For example this code will make an error box and interrupt users. function getDomainFromURL(url) { assertTrue(url, 'URL should not be null'); ...parsing } So, I'd make like this. function getDomainFromURL(url) { if (!url) return; ...parsing } Second one is good for usability, I think, and

Assertive programming with JavaScript

泄露秘密 提交于 2020-01-22 13:43:27
问题 I know why assertive programming is good, so I want to use it with JavaScript. However, I don't want to show users error boxes, and it's unusual thing. Just ignore it and make them retry could be better. For example this code will make an error box and interrupt users. function getDomainFromURL(url) { assertTrue(url, 'URL should not be null'); ...parsing } So, I'd make like this. function getDomainFromURL(url) { if (!url) return; ...parsing } Second one is good for usability, I think, and

allure的其他参数

烈酒焚心 提交于 2020-01-21 23:28:03
import pytestimport requestsimport allure l = [ { "url": "https://www.v2ex.com/api/site/info.json", "title":"v2ex的title", "desc":"v2ex的描述信息", "expect": {"title": "V2EX","slogan1111":"way to explore","domain":"www.v2ex.com"} }, { "url": "https://cnodejs.org/api/vl/topics", "title":"cnodejs的title", "desc":"cnodejs的描述", "expect": {"success": True}},]@pytest.mark.parametrize("d", l)def test_case(d): #allure的动态参数 dynamic allure.dynamic.title(d["title"]) allure.dynamic.description(d["desc"]) response = requests.get(url=d["url"]).json() for k in d["expect"]: print(333333,k) if d["expect"][k] !=