GTest

example

https://cmake.org/cmake/help/latest/module/FetchContent.html

https://cmake.org/cmake/help/git-stage/module/GoogleTest.html

xUnit architecture
International Software Testing Qualifications Board

ctest 是 命令

1
2
3
$ cmake -S . -B build
$ cmake --build build
$ cd build && ctest
  1. Test

Google 以前用来代表 test case 的术语

  1. Test Case

TEST() 代表 test case, Exercise a particular program path with specific input values and verify the results

  1. Test Suite

Google 以前吧关联的测试用例叫 test case, 现在逐步采用 suite API refactor,deprecate 老的 API,A test suite contains one or many tests.

assertion 三种结果,成功,非致命性错误(nonfatal failure),致命性错误(fatal failure),本质是函数

测试崩溃或 expect 错误会失败

ASSERT_* 会产生致命性错误,中止当前的函数往下走,EXPECT_* 则不会产生致命性错误,还会继续走,一般使用 expect 来发现更多错误,没有意义往下走使用 assert

assert 会直接返回,所以不执行 clean-up 代码,会导致泄漏,如果发生 heap check error 的时候,要考虑是否是 assert 导致的
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.

自定义 log ostream,会被自动转为 UTF-8 编码

ASSERT_EQ(x.size(), y.size()) << “Vectors x and y are of unequal length”;

TEST(TestSuiteName, TestName) {
… test body …
}
有问题就会失败

测试全名是 suite + test,两个名字不应该加下划线,全名是 identifier,相同的第一个名字应该相同

capi

本文主要介绍车载以太网的 CommonAPI 的应用

Read more

git

本文主要记录 git 使用。submodule

Read more

Markdown

What is Markdown?

Markdown 是一种轻量级的文本标记写法(markup language),广泛用于 GitHub,博客等各种 Web 网站。Markdown guide 网站源码:GitHub,网站教程已经写的很好,本文借鉴于此,做个笔记

原理

处理 markdown 文件的应用程序使用一种称为 Markdown 处理器,也叫解析器或者实现的东西将该格式文件转换为 HTML, 再通过 Web 浏览器进行显示

Why use Markdown?

  1. easy to learn, use, light-weight

  2. place emphasis on content, not mark format

  3. can be transfer into html or other format, easy for distribution

  4. 微软的 word 等软件太生草了,写的时候纠结于格式,打开还要下载特定的软件,不适合到处纷发这种特性,Markdown 注重的是写作的内容而不是格式问题

  5. Markdown 可以内嵌 HTML 语法,许多网站内嵌 Markdown 处理器,适合现在 Web 浏览器显示

生成 GitHub Pages

GitHub 为 Jekyll 提供免费的网页 host 服务

GitHub Pages uses Jekyll as the backend for its free website creation service

其他可用的静态网站生成器

如果您想使用内容管理系统(CMS)来为网站提供动力,请查看Ghost。这是一个免费的开源博客平台,具有出色的Markdown编辑器。如果您是WordPress用户,您将很高兴知道WordPress.com上托管的网站有Markdown支持。自托管的WordPress网站可以使用Jetpack插件

How to use Markdown?

Basic Syntax

  1. 标题

    1
    2
    3
    4
    5
    6
    7
    8
    # 标题
    or
    <h1></h1>
    <h2></h2>

    段落之间空行,且不要缩进。用 &nbsp;
    <p></p>
    <br>
  2. 粗体

    1
    2
    3
    **bold**
    or
    <strong></strong>
  3. 斜体

    1
    2
    3
    *italic*
    or
    <em></em>
  4. 块引用

    1
    2
    3
    4
    5
    6
    > blockquote
    >> two
    之间空行
    > ###
    >
    > -
  5. 有序列表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    1. ordered list
    2. two

    <ol>
    <li></li>
    <li></li>
    </ol>

    1968\. 转义

    在列表中通过缩进四个空格来添加其他元素
  6. 无序列表

    1
    2
    3
    4
    5
    - unordered list
    <ul>
    <li></li>
    <li></li>
    </ul>
  7. code

    1
    2
    3
    `code`
    <code></code>
    ``Use `code` in your Markdown file.``
  8. 水平线 horizontal rule

    1
    2
    ---
    之间空行
  9. 链接

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [title](link address "悬停提示")

    直接显示链接
    <https://www.markdownguide.org>
    <fake@example.com>

    I love supporting the **[EFF](https://eff.org)**.
    This is the *[Markdown Guide](https://www.markdownguide.org)*.
    See the section on [`code`](#code).

    <a href="" title="">link</a>
    与原来的链接一样,1 为 1 的链接
    [hobbit-hole][1]
    [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle "Hobbit lifestyles"
    Markdown 中链接如果有空格用%20代替,否则用 HTML 的 tag
  10. 图片

    1
    2
    3
    ![alt 不显示的时候](image.jpg "title")

    [![An old rock in the desert](/assets/images/shiprock.jpg "Shiprock, New Mexico by Beau Rogers")](https://www.flickr.com/photos/)

块级元素之间分行,尽量不要缩进 tag,会干扰格式,且不能在块级元素使用 Markdown 语法,不会工作

\进行转义

Extended Syntax

GitHub Flavored Markdown (GFM)

  1. 表格 table

    1
    2
    3
    4
    5
    | class | name | test |
    |:------|:----:|-----:|
    | 1 | 2 | \| |

    | 可以用 &#124; 表中可以用 HTML 语法加入 list
  2. 代码块 fenced code block

    1
    2
    3
    4
    5
    6
    7
    ```json
    {
    "firstname": "John",
    "lastname": "Smith",
    "age": 25
    }
    \`\`\`
  3. 脚注 footnote

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    可以添加标识符,但实际上仍然是按顺序标号
    Here's a simple footnote,[^1] and here's a longer one.[^bignote]

    [^1]: This is the first footnote.

    [^bignote]: Here's one with multiple paragraphs and code.

    Indent paragraphs to include them in the footnote.

    `{ my code }`

    Add as many paragraphs as you like.
  4. 标题 ID heading ID

    1
    2
    3
    4
    5
    ### My Great Heading {#custom-id}
    <h3 id="custom-id">My Great Heading</h3>
    [Heading IDs](#heading-ids)
    <a href="#heading-ids">Heading IDs</a>
    [Heading IDs](https://www.markdownguide.org/extended-syntax#heading-ids)
  5. 定义列表 definition list

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    term
    : definitionFirst Term
    : This is the definition of the first term.

    Second Term
    : This is one definition of the second term.
    : This is another definition of the second term.

    <dl>
    <dt>First Term</dt>
    <dd>This is the definition of the first term.</dd>
    <dt>Second Term</dt>
    <dd>This is one definition of the second term. </dd>
    <dd>This is another definition of the second term.</dd>
    </dl>
  6. 删除线 strikethrough

    1
    ~~The world is flat.~~
  7. 任务列表 task list

    1
    2
    - [x]
    - [ ]
  8. 表情 emoji

    Emojipedia

    list of emoji shortcodes

    1
    That is so funny! :joy:
  9. 高亮 highlight

    1
    2
    I need to highlight these ==very important words==.
    <mark></mark>
  10. 下标 subscript

    1
    2
    H~2~0
    <sub>2</sub>
  11. 上标 superscript

    1
    2
    X^2^
    <sup>2</sup>

diable automatic URL linking

1
`http://www.example.com`

Hacks

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 下划线 underline
<ins></ins>

# indent(Tab)
&nbsp; non-breaking space

# center
<p style="text-align:center">Center this text</p>

# color
<p style="color:blue">Make this text blue.</p>

# comment
[This is a comment that will be hidden.]: #

# 告诫 admonitions
> :warning: **Warning:** Do not push the big red button.

> :memo: **Note:** Sunrises are beautiful.

> :bulb: **Tip:** Remember to appreciate the little things in life.

# image size
<img src="image.png" width="200" height="100">

# 图像说明 image captions
<figure>
<img src="/assets/images/albuquerque.jpg"
alt="Albuquerque, New Mexico">
<figcaption>A single track trail outside of Albuquerque, New Mexico.</figcaption>
</figure>

or

![Albuquerque, New Mexico](/assets/images/albuquerque.jpg)
*A single track trail outside of Albuquerque, New Mexico.*

# link target
<a href="https://www.markdownguide.org" target="_blank">Learn Markdown!</a>


# symbols
Copyright (©) — &copy;
Registered trademark (®) — &reg;
Trademark (™) — &trade;
Euro (€) — &euro;
Left arrow (←) — &larr;
Up arrow (↑) — &uarr;
Right arrow (→) — &rarr;
Down arrow (↓) — &darr;
Degree (°) — &#176;
Pi (π) — &#960;

# table 里的元素
<br>
list

# table of contents
- [Underline](#underline)
- [Indent](#indent)
- [Center](#center)
- [Color](#color)

# video
YouTube automatically generates an image for every video (https://img.youtube.com/vi/YOUTUBE-ID/0.jpg), so we can use that and link the image to the video on YouTube. After we replace the image alt text and add the ID of the video, our example looks like this:
[![Less Than Jake — Scott Farcas Takes It On The Chin](https://img.youtube.com/vi/PYCxct2e0zI/0.jpg)](https://www.youtube.com/watch?v=PYCxct2e0zI)

Redis

release: latest

data structure sds in redis

sds: simple dynamic string. 类型 sds 的声明为

1
typedef char*sds;

为 sds 申请动态内存的时候,申请了一块连续内存,该内存头部包含了 sds 的头部信息,并且维护该头部信息,源码里称为 sds_hdr_var, sds的 header variable

最新版本的 redis 一共实现了五种相似的 sds 结构体,分别为 sdshdr5(不建议使用), sdshdr8, sdshdr16, sdshdr32, sdshdr64,以 sdshdr5 和 sdshdr8 为例

1
2
3
4
5
6
7
8
9
10
struct __attribute__ ((__packed__)) sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {
uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
  1. struct 声明中使用了 GNU C 编译器的结构体注释,放弃编译器对字节对齐的优化;
  2. 变量 flags 中 3 lsb (低三位) 表示了该结构体变量的类型,一共五种,所以只需要低三位;
  3. 变量 len 为该 string 已经使用的字符串长度,不包含 C 风格字符串的 ‘\000’;
  4. 变量 alloc 为该 string 分配的字符串长度,包含了 C 风格字符串中的 ‘\000’,
  5. 变量 buf 为空,实际上代表结构体 char *sds 的位置,与头部信息相连。
1
2
3
4
5
6
#define SDS_TYPE_5  0
#define SDS_TYPE_8 1
#define SDS_TYPE_16 2
#define SDS_TYPE_32 3
#define SDS_TYPE_64 4
#define SDS_TYPE_MASK 7

宏变量,方便进行位操作

1
2
#define SDS_TYPE_BITS 3
#define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)

宏变量,低三位,移位可以获取 type

1
2
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));
#define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))

第一个宏函数,声明一个指针 sh,指向 sdshdr##T 类型,sds 变量 s 的指针,该指针指向头部信息

第二个宏函数,同第一个,但不显示声明指针,只是获取地址

通过位操作位偏移操作,可以非常方便的获取 sds 变量的头部信息

1
2
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK)

优点

保存了 string 信息,二进制安全,同时使用了大量位操作,加快运算。

sds 中保存的实际上是 C 风格字符串,可以使用 C 标准库接口

sds 模块依赖

1
2
sds.o: sds.c sds.h sdsalloc.h zmalloc.h \
../deps/jemalloc/include/jemalloc/jemalloc.h

依赖为 redis 实现的 zmalloc,zmalloc 是 redis 为 jemalloc 封装的自己内部模块的申请动态内存模块,jemalloc 为优秀的用于高并发用于替代 malloc 的开源库

sdsalloc.h 为 sds 模块与 zmalloc 模块的定义接口文件

Docker

docker cheatsheet

Read more