赞
踩
layout | title | categories | tags | date | description | |||
---|---|---|---|---|---|---|---|---|
post
|
《高质量的C++代码笔记》
|
|
|
2018-11-11 12:59:59 -0800
|
软件质量是被大多数程序员挂在嘴上而不是放在心上的东西!有多少软件开发人员对正确性、健壮性、可靠性、效率、易用性、可读性(可理解性)、可扩展性、可复用性、兼容性、可移植性等质量属性了如指掌?并且能在实践中运用自如?。
|
软件质量是被大多数程序员挂在嘴上而不是放在心上的东西! 除了完全外行和真正的编程高手外,初读本书,你最先的感受将是惊慌:“哇!我 以前捏造的 C++/C 程序怎么会有那么多的毛病?”有多少软件开发人员对正确性、健壮性、可靠性、效率、易用性、可读性(可理解性)、可扩展性、可复用性、兼容性、可移植性等质量属性了如指掌?并且能在实践中运用自如?。至少我现在还不是,我只能将平时遇到的一些值得记录的东西记录下来,以供下次翻阅。
strcpy
的实现可以看出一个人的
- char* strcpy(char* dest, const char* source)
- {
- char * destcopy = dest;
- if((dest == NULL) || (source == NULL))
- throw "Invalid Arguments";
- while((*dest++=*source++)!= '\0');
- return destcopy;
- }
- /*
- * Copyright (c) 2001,上海贝尔有限公司网络应用事业部
- * All rights reserved.
- *
- * 文件名称: filename.h
-
- * 文件标识: 见配置管理计划书
- * 摘 要: 简要描述本文件的内容
- *
- * 当前版本: 1.1
- * 作 者: 输入作者(或修改者)名字
- * 完成日期: 2001年7月20日
- *
- * 取代版本: 1.0
- * 原作者 : 输入原作者(或修改者)名字
- * 完成日期: 2001年5月10日
- */
修饰符 * 和 & 应该靠近数据类型还是该靠近变量名,是个有争议的活题。 若将修饰符 * 靠近数据类型,例如: int* x; 从语义上讲此写法比较直观,即 x 是 int 类型的指针。 上述写法的弊端是容易引起误解,例如: int* x, y; 此处 y 容易被误解为指针变 量。虽然将 x 和 y 分行定义可以避免误解,但并不是人人都愿意这样做。
unix系统中常常采用小写字母+_ 的方式 g_:全局变量 k_:static 变量 m_:class成员变量
每个类只有一个析构函数和一个赋值函数,但可以有多个构造函数(包含一个拷贝 构造函数,其它的称为普通构造函数)。对于任意一个类 A,如果不想编写上述函数, C++编译器将自动为 A 产生四个缺省的函数,如 A(void);
// 缺省的无参数构造函数 A(const A &a);
// 缺省的拷贝构造函数 ~A(void);
// 缺省的析构函数 A & operate =(const A &a);
// 缺省的赋值函数
不少难以察觉的程序错误是由于变量没有被正确初始化或清除造成的,而初始化和清除工作很容易被人遗忘。
#define stub fprintf(stderr, "error param in %s:%s:%d\n", __FUNCTION__, __FILE__, __LINE__);
这是因为结构体内存分配有自己的对齐规则,结构体内存对齐默认的规则如下:
内存对齐https://www.cnblogs.com/suntp/p/MemAlignment.html
OpenCV中16b对齐的内存申请和释放
- #define CV_MALLOC_ALIGN 16
- /*!
- Aligns pointer by the certain number of bytes
- This small inline function aligns the pointer by the certian number of bytes by shifting
- it forward by 0 or a positive offset.
- */
- template <typename _Tp>
- static inline _Tp* alignPtr(_Tp* ptr, int n=(int)sizeof(_Tp))
- {
- return (_Tp*)(((size_t)ptr + n-1) & -n);
- }
-
- /*!
- Aligns buffer size by the certain number of bytes
- This small inline function aligns a buffer size by the certian number of bytes by enlarging it.
- */
- static inline size_t alignSize(size_t sz, int n)
- {
- assert((n & (n - 1)) == 0); // n is a power of 2
- return (sz + n-1) & -n;
- }
-
- void* fastMalloc( size_t size )
- {
- uchar* udata = (uchar*)malloc(size + sizeof(void*) + CV_MALLOC_ALIGN);
- if(!udata)
- return OutOfMemoryError(size);
- uchar** adata = alignPtr((uchar**)udata + 1, CV_MALLOC_ALIGN);
- adata[-1] = udata;
- return adata;
- }
-
- void fastFree(void* ptr)
- {
- if(ptr) {
- uchar* udata = ((uchar**)ptr)[-1];
- CV_DbgAssert(udata < (uchar*)ptr &&
- ((uchar*)ptr - udata) <= (ptrdiff_t)(sizeof(void*)+CV_MALLOC_ALIGN));
- free(udata);
- }
- }
- #include <cstdio>
- #include <cstring>
- ///@brief 模型配置结构体
- ///@warning 该结构体在与did_model_set_config一起使用时,一定要先全部填充为0,再设置所需要的field。
- /// set_model_config/get_model_config 是对该结构体的浅拷贝
- typedef struct net_config_t {
- int engine;
-
- ///@brief each engine specific data can be passed to here, such as snpe_runtime, tensorrt_batch_size, ocl_context and so on.
- ///@note each engine implementation will cast it to the corresponding runtime type, such as snpe_context_t, ppl_context_t.
- /// The lifetime of this pointer should span until create_handle finished, and the memory is managed by users.
- void* engine_context;
- } net_config_t;
-
- void set_net_config_t(net_config_t* config, int engine_type) {
- memset(config, 0, sizeof(net_config_t));
- int otherc[] = {1, 0, 5};
- config->engine = engine_type;
- config->engine_context = (void*)&otherc;
- }
-
- int main(int argc, char* argv[]) {
- net_config_t config;
- // 设置模型加载配置项
- set_net_config_t(&config, 3);
- fprintf(stderr, "config.engine %d\n", config.engine);
- int* context = (int*)config.engine_context;
- fprintf(stderr, "config.engine_context[0]=%d\n", context[0]);
- fprintf(stderr, "config.engine_context[1]=%d\n", context[1]);
- fprintf(stderr, "config.engine_context[2]=%d\n", context[2]);
- return 0;
- }
第一次运行
config.engine 3 config.engine_context[0]=1286489600 config.engine_context[1]=32624 config.engine_context[2]=1288667592
第二次运行
config.engine 3 config.engine_context[0]=-204200448 config.engine_context[1]=32695 config.engine_context[2]=-202022456
从结果中可以看出engine_context中的内存是一块未初始化的内存空间。这是因为返回的局部数组被释放导致的结果。 这情况可能导致你的程序有不期望的执行结果。尤其是如果采用context[1]作为分支判断条件,本来应该为0或者false, 现在可能是正数,也可能是负数,为0的概率非常小。因此我们要避免这种返回局部变量的情况。
方案 1
- // A macro to disallow copy constructor and operator=
- // This should be used in the private: declarations for a class.
- #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
- type(type const &);\
- void operator=(type const &)
-
- class TestFactoryBase
- {
- private:
- GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase);
- }
方案2
- class P {
- public:
- P(const P &) = delete;
- P &operator =(const P &p) = delete;
- };
以上两个delete声明禁止复制 能够通过明确的方式显式限定这些特殊方法有助于增强代码的可读性和可维护性
- ClassName& operator++()
- {
- ++cur;
- if(cur == last)
- {
- set_node(node + 1);
- cur = first;
- }
- return *this;
- }
-
- ClassName operator(int)
- {
- ClassName tmp = *this;
- ++*this;
- return tmp;
- }
u32Width是unsigned int类型的,在进行计算过程中如果u32Width=2
,执行到for (; j <= u32Width - 4; j += 4)
的时候,会出现问题: 由于j是size_t类型的, u32Width-4
会被转化为unsigned int
类型,从而造成该判断可通过,从直观上看来就发生了 j <= -2
(实际是j <= 4294967294
)是为true
的事情了。
- const unsigned int blob_len = u32Num * u32Chn * u32Height;
- for (size_t i = 0; i < blob_len; ++i) {
- size_t j = 0;
- for (; j <= u32Width - 4; j += 4) {
- dataDstBlob[j] = (dst_type)(ps32Ptr[j] * NNIE_DATA_SCALE_INV);
- dataDstBlob[j + 1] = (dst_type)(ps32Ptr[j + 1] * NNIE_DATA_SCALE_INV);
- dataDstBlob[j + 2] = (dst_type)(ps32Ptr[j + 2] * NNIE_DATA_SCALE_INV);
- dataDstBlob[j + 3] = (dst_type)(ps32Ptr[j + 3] * NNIE_DATA_SCALE_INV);
- }
- for (; j < u32Width; ++j) {
- dataDstBlob[j] = (dst_type)(ps32Ptr[j] * NNIE_DATA_SCALE_INV);
- }
- dataDstBlob += u32Width;
- ps32Ptr += blob->u32Stride / getElemSize(blob->enType);
- }
bitset是处理进制转换,基于bit的算法中简单算法,虽然也可以使用raw的char array替代,但是很多bitset自带的方法,可以让程序飞起来。
- #include <iostream>
- #include <bitset>
- using namespace std;
- void increment(std::bitset<5>& bset)
- {
- for (int i = 0; i < 5; ++i)
- {
- if(bset[i] == 1)
- bset[i] = 0;
- else
- {
- bset[i] = 1;
- break;
- }
- }
-
- }
- void method_1(){
- for (int i = 0; i < 32; ++i) {
- std::bitset<5> bset(i);
- std::cout << bset << std::endl;
- }
- }
- int main(int argc, char const *argv[]){
- std::bitset<5> bset(0);
- for (int i = 0; i < 32; ++i) {
- std::cout << bset << std::endl;
- increment(bset);
- }
-
- return 0;
- }
仿函数(functor),就是使一个类的使用看上去象一个函数。其实现就是类中实现一个 operator(),这个类就有了类似函数的行为,就是一个仿函数类了。C语言使用函数指针和回调函数来实现仿函数,例如一个用来排序的函数可以这样使用仿函数.在C++里,我们通过在一个类中重载括号运算符的方法使用一个函数对象而不是一个普通函数。
- template <typename T>
- struct xxx
- {
- returnType operator()(const T& x)
- {
- return returnType;
- }
- }
-
-
- template<typename T>
- class display
- {
- public:
- void operator()(const T &x)
- {
- cout<<x<<" ";
- }
- };
- #include <stdio.h>
- #include <stdlib.h>
- //int sort_function( const void *a, const void *b);
- int sort_function( const void *a, const void *b)
- {
- return *(int*)a-*(int*)b;
- }
-
- int main()
- {
-
- int list[5] = { 54, 21, 11, 67, 22 };
- qsort((void *)list, 5, sizeof(list[0]), sort_function);//起始地址,个数,元素大小,回调函数
- int x;
- for (x = 0; x < 5; x++)
- printf("%i\n", list[x]);
-
- return 0;
- }
要使用STL内建的仿函数,必须包含头文件。而头文件中包含的仿函数分类包括
- #include <iostream>
- #include <numeric>
- #include <vector>
- #include <functional>
- using namespace std;
-
- int main()
- {
- int ia[]={1,2,3,4,5};
- vector<int> iv(ia,ia+5);
- cout<<accumulate(iv.begin(),iv.end(),1,multiplies<int>())<<endl;
-
- cout<<multiplies<int>()(3,5)<<endl;
-
- modulus<int> modulusObj;
- cout<<modulusObj(3,5)<<endl; // 3
- return 0;
- }
等于:equal_to 不等于:not_equal_to 大于:greater 大于等于:greater_equal 小于:less 小于等于:less_equal
从大到小排序:
- #include <iostream>
- #include <algorithm>
- #include <vector>
-
- using namespace std;
-
- template <class T>
- class display
- {
- public:
- void operator()(const T &x)
- {
- cout<<x<<" ";
- }
- };
-
- int main()
- {
- int ia[]={1,5,4,3,2};
- vector<int> iv(ia,ia+5);
- sort(iv.begin(),iv.end(),greater<int>());
- for_each(iv.begin(),iv.end(),display<int>());
- return 0;
- }
逻辑与:logical_and 逻辑或:logical_or 逻辑否:logical_no
TEST_F与TEST的区别是,TEST_F提供了一个初始化函数(SetUp)和一个清理函数(TearDown),在TEST_F中使用的变量可以在初始化函数SetUp中初始化,在TearDown中销毁,并且所有的TEST_F是互相独立的,都是在初始化以后的状态开始运行,一个TEST_F不会影响另一个TEST_F所使用的数据。
- //A.h
- #ifndef A_H
- #define A_H
- class A
- {
- private:
- int _a;
- public:
- A( int a );
- ~A( );
- void add( int a );
- int getA( );
- };
- #endif
- A.cpp
- #include "A.h"
- A::A( int a ){
- this->_a = a;
- }
- A::~A( ){
- }
- void A::add( int a ){
- this->_a += a;
- }
- int A::getA( ){
- return this->_a;
- }
- // A_test.cpp
- #include "A.h"
- #include <gtest/gtest.h>
- class A_test : public testing::Test {
- protected:
- A* _p_a;
- virtual void SetUp( ){ //初始化函数
- this->_p_a = new A( 1 );
- }
- virtual void TearDown( ){ //清理函数
- delete this->_p_a;
- }
- };
- //第一个测试,参数A_test是上面的那个类,第二个参数FirstAdd是测试名称
- TEST_F( A_test,FirstAdd ){
- EXPECT_EQ( 1,_p_a->getA( ) );
- _p_a->add( 3 );
- EXPECT_EQ( 4,_p_a->getA( ) );
- }
-
- //第二个测试
- TEST_F( A_test,SecondAdd ){
- EXPECT_EQ( 1,_p_a->getA( ) );
- _p_a->add( 5 );
- EXPECT_EQ( 6,_p_a->getA( ) );
- }
-
- /*
- 上面的两个测试都是在SetUp函数执行后的状态下执行,也就是说在执行任意一个TEST_F时 _p_a->_a 的值都是初始值1
- */
- #include <gtest/gtest.h>
-
- int main(int argc, char * argv[])
- {
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
- }
- #include <unistd.h>
- #include <sys/time.h>
- #include <time.h>
-
- #define __TIC__() \
- struct timeval __timing_start, __timing_end; \
- gettimeofday(&__timing_start, NULL);
-
- #define __TOC__() \
- do { \
- gettimeofday(&__timing_end, NULL); \
- double __timing_gap = (__timing_end.tv_sec - \
- __timing_start.tv_sec) * \
- 1000.0 + \
- (__timing_end.tv_usec - \
- __timing_start.tv_usec) / \
- 1000.0; \
- fprintf(stdout, "TIME(ms): %lf\n", __timing_gap); \
- } while (0)
- //第一个测试,参数A_test是上面的那个类,第二个参数FirstAdd是测试名称
- TEST(A_test, FirstAdd){
- EXPECT_EQ( 1,_p_a->getA( ) );
- _p_a->add( 3 );
- EXPECT_EQ( 4,_p_a->getA( ) );
- }
-
- // Define this macro to 1 to omit the definition of TEST(), which
- // is a generic name and clashes with some other libraries.
- #if !GTEST_DONT_DEFINE_TEST
- # define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)
- #endif
-
- #define GTEST_TEST(test_case_name, test_name)\
- GTEST_TEST_(test_case_name, test_name, \
- ::testing::Test, ::testing::internal::GetTestTypeId())
- // Expands to the name of the class that implements the given test.
- #define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \
- test_case_name##_##test_name##_Test
-
- // Helper macro for defining tests.
- // 这个宏声明了一个继承自parent_class ::testing::Test的类,然后对这个类的属性test_info_进行赋值
-
- #define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\
- class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\
- public:\
- GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\
- private:\
- virtual void TestBody();\
- static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\
- GTEST_DISALLOW_COPY_AND_ASSIGN_(\
- GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\
- };\
- /*这个宏声明了一个继承自parent_class ::testing::Test的类,然后对这个类的属性test_info_进行赋值*/\
- ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\
- ::test_info_ =\
- ::testing::internal::MakeAndRegisterTestInfo(\
- #test_case_name, #test_name, NULL, NULL, \
- (parent_id), \
- parent_class::SetUpTestCase, \
- parent_class::TearDownTestCase, \
- new ::testing::internal::TestFactoryImpl<\
- GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\
- // 实现我们的这个TestBody\
- void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()
- TestInfo* MakeAndRegisterTestInfo(
- const char* test_case_name,
- const char* name,
- const char* type_param,
- const char* value_param,
- TypeId fixture_class_id,
- SetUpTestCaseFunc set_up_tc,
- TearDownTestCaseFunc tear_down_tc,
- TestFactoryBase* factory) {
- TestInfo* const test_info =
- new TestInfo(test_case_name, name, type_param, value_param,
- fixture_class_id, factory);
- // 添加测试用例信息到UnitTestImpl的testcase_中
- GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info);
- return test_info;
- }
- // Finds and returns a TestCase with the given name. If one doesn't
- // exist, creates one and returns it. It's the CALLER'S
- // RESPONSIBILITY to ensure that this function is only called WHEN THE
- // TESTS ARE NOT SHUFFLED.
- //
- // Arguments:
- //
- // test_case_name: name of the test case
- // type_param: the name of the test case's type parameter, or NULL if
- // this is not a typed or a type-parameterized test case.
- // set_up_tc: pointer to the function that sets up the test case
- // tear_down_tc: pointer to the function that tears down the test case
- TestCase* UnitTestImpl::GetTestCase(const char* test_case_name,
- const char* type_param,
- Test::SetUpTestCaseFunc set_up_tc,
- Test::TearDownTestCaseFunc tear_down_tc) {
- // Can we find a TestCase with the given name?
- const std::vector<TestCase*>::const_iterator test_case =
- std::find_if(test_cases_.begin(), test_cases_.end(),
- TestCaseNameIs(test_case_name));
-
- if (test_case != test_cases_.end())
- return *test_case;
-
- // No. Let's create one.
- TestCase* const new_test_case =
- new TestCase(test_case_name, type_param, set_up_tc, tear_down_tc);
-
- // Is this a death test case?
- if (internal::UnitTestOptions::MatchesFilter(test_case_name,
- kDeathTestCaseFilter)) {
- // Yes. Inserts the test case after the last death test case
- // defined so far. This only works when the test cases haven't
- // been shuffled. Otherwise we may end up running a death test
- // after a non-death test.
- ++last_death_test_case_;
- test_cases_.insert(test_cases_.begin() + last_death_test_case_,
- new_test_case);
- } else {
- // No. Appends to the end of the list.
- test_cases_.push_back(new_test_case);
- }
-
- test_case_indices_.push_back(static_cast<int>(test_case_indices_.size()));
- return new_test_case;
- }
Google C++ Testing Framework helps you write better C++ tests.
No matter whether you work on Linux, Windows, or a Mac, if you write C++ code, Google Test can help you.
So what makes a good test, and how does Google C++ Testing Framework fit in? We believe:
Since Google C++ Testing Framework is based on the popular xUnit architecture, you'll feel right at home if you've used JUnit or PyUnit before. If not, it will take you about 10 minutes to learn the basics and get started. So let's go!
Note: We sometimes refer to Google C++ Testing Framework informally as Google Test.
To write a test program using Google Test, you need to compile Google Test into a library and link your test with it. We provide build files for some popular build systems: msvc/
for Visual Studio, xcode/
for Mac Xcode, make/
for GNU make, codegear/
for Borland C++ Builder, and the autotools script (deprecated) and CMakeLists.txt
for CMake (recommended) in the Google Test root directory. If your build system is not on this list, you can take a look at make/Makefile
to learn how Google Test should be compiled (basically you want to compile src/gtest-all.cc
with GTEST_ROOT
and GTEST_ROOT/include
in the header search path, where GTEST_ROOT
is the Google Test root directory).
Once you are able to compile the Google Test library, you should create a project or build target for your test program. Make sure you have GTEST_ROOT/include
in the header search path so that the compiler can find "gtest/gtest.h"
when compiling your test. Set up your test project to link with the Google Test library (for example, in Visual Studio, this is done by adding a dependency on gtest.vcproj
).
If you still have questions, take a look at how Google Test's own tests are built and use them as examples.
When using Google Test, you start by writing assertions, which are statements that check whether a condition is true. An assertion's result can be success, nonfatal failure, or fatal failure. If a fatal failure occurs, it aborts the current function; otherwise the program continues normally.
Tests use assertions to verify the tested code's behavior. If a test crashes or has a failed assertion, then it fails; otherwise it succeeds.
A test case contains one or many tests. You should group your tests into test cases that reflect the structure of the tested code. When multiple tests in a test case need to share common objects and subroutines, you can put them into a test fixture class.
A test program can contain multiple test cases.
We'll now explain how to write a test program, starting at the individual assertion level and building up to tests and test cases.
Google Test assertions are macros that resemble function calls. You test a class or function by making assertions about its behavior. When an assertion fails, Google Test prints the assertion's source file and line number location, along with a failure message. You may also supply a custom failure message which will be appended to Google Test's message.
The assertions come in pairs that test the same thing but have different effects on the current function. ASSERT_*
versions generate fatal failures when they fail, and abort the current function. EXPECT_*
versions generate nonfatal failures, which don't abort the current function. Usually EXPECT_*
are preferred, as they allow more than one failures to be reported in a test. However, you should use ASSERT_*
if it doesn't make sense to continue when the assertion in question fails.
Since a failed ASSERT_*
returns from the current function immediately, possibly skipping clean-up code that comes after it, it may cause a space leak. Depending on the nature of the leak, it may or may not be worth fixing - so keep this in mind if you get a heap checker error in addition to assertion errors.
To provide a custom failure message, simply stream it into the macro using the <<
operator, or a sequence of such operators. An example:
- ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";
-
- for (int i = 0; i < x.size(); ++i) {
- EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
- }
Anything that can be streamed to an ostream
can be streamed to an assertion macro--in particular, C strings and string
objects. If a wide string (wchar_t*
, TCHAR*
in UNICODE
mode on Windows, or std::wstring
) is streamed to an assertion, it will be translated to UTF-8 when printed.
These assertions do basic true/false condition testing.
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_TRUE( condition) ; |
EXPECT_TRUE( condition) ; |
condition is true |
ASSERT_FALSE( condition) ; |
EXPECT_FALSE( condition) ; |
condition is false |
Remember, when they fail, ASSERT_*
yields a fatal failure and returns from the current function, while EXPECT_*
yields a nonfatal failure, allowing the function to continue running. In either case, an assertion failure means its containing test fails.
Availability: Linux, Windows, Mac.
This section describes assertions that compare two values.
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_EQ( val1, val2); |
EXPECT_EQ( val1, val2); |
val1 == val2 |
ASSERT_NE( val1, val2); |
EXPECT_NE( val1, val2); |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。