sqlite3

什么是SQLite

SQLite是一个进程内的轻量级数据库,它实现了自给自足的、无服务器的、零配置的、事务性的SQL数据库引擎。它是一个零配置的数据库,也就意味着与其它数据库不一样,我们不需要在系统中配置。像其他数据库,SQLite引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接,SQLite直接访问其存储文件。

为什么使用SQLite

不需要一个单独的服务器进程或操作系统。
不需要配置
一个完整的SQLite数据库是存储在一个单一的跨平台的磁盘文件

SQLite3 C/C++API 介绍

C/C++ API是SQLite3数据库的一个客户端,提供一种用C/C++操作数据库的方法。
介绍几个接口:
官方文档:https://www.sqlite.org/c3ref/funclist.html
查看当前数据库在编译阶段是否启动了线程安全
int sqlite3_threadsafe():0-未启用: 1-启用

1
2
3
4
5
6
7
8
9
10
11
int sqlite3_open(const char *filename, sqlite3 **ppDb) 成功返回SQLITE_OK
//若在编译阶段启动了线程安全,则在程序运行阶段可以通过参数选择线程安全等级
int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs );
flag:
SQLITE_OPEN_READWRITE -- 以可读可写方式打开数据库文件
SQLITE_OPEN_CREATE -- 不存在数据库文件则创建
SQLITE_OPEN_NOMUTEX--多线程模式,只要不同的线程使用不同的连接即可保证线程安全
SQLITE_OPEN_FULLMUTEX--串行化模式

返回:SQLITE_OK 表示成功

gtest

使用

1
2
TEST(test_case_name,test_name)
TEST_F(test_fixture,test_name)

TEST:主要用来创建一个简单测试,它定义一个测试函数,在这个函数中可以用任何C++代码并且使用框架提供的断言进行检查。
TEST_F:主要用来多样测试,适用于多个测试场景如果需要相同配置的情况,即相同的数据测不同的行为。

断言

GTest中的断言可以分为两类:

  • ASSERT_系列:如果当前点检测失败则退出当前函数。
  • EXPECT_系列:如果当前点检测失败则继续向下执行
    介绍:
    1
    2
    3
    4
    5
    6
    7
    8
    ASSERT_EQ(参数 1,参数 2),传入的是需要比较的两个数 equal
    ASSERT_NE(参数 1,参数 2),not equal,不等于才返回 true
    ASSERT_LT(参数 1,参数 2),less than,小于才返回 true
    ASSERT_GT(参数 1,参数 2),greater than,大于才返回 true

    ASSERT_LE(参数 1,参数 2),less equal,小于等于才返回 true

    ASSERT_GE(参数 1,参数 2),greater equal,大于等于才返回 true
    案例:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38

    #include <iostream>
    #include <gtest/gtest.h>
    #include <unordered_map>
    #include <string>
    using namespace std;
    std::unordered_map<std::string, std::string> mmp;

    class MyTest : public testing::Test
    {
    public:
        void SetUp() override
        {
            std::cout << "单元测试环境初始化:" << std::endl;
            mmp.insert(make_pair("你哈", "hello"));
            mmp.insert(make_pair("五花肉", "keyi"));
        }

        void TearDown() override
        {
            cout << "单元测试完毕后的环境清理!" << endl;
            mmp.clear();
        }
    };
    TEST_F(MyTest,test1)
    {
        ASSERT_EQ(mmp.size(),2);
        mmp.erase("hello");
    }
    TEST_F(MyTest,test2)
    {
        ASSERT_EQ(mmp.size(),2);
    }
    int main(int argc, char **argv)
    {
        testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from MyTest
[ RUN ] MyTest.test1
单元测试环境初始化:
单元测试完毕后的环境清理!
[ OK ] MyTest.test1 (0 ms)
[ RUN ] MyTest.test2
单元测试环境初始化:
单元测试完毕后的环境清理!
[ OK ] MyTest.test2 (0 ms)
[----------] 2 tests from MyTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 2 tests.