CARLA
 
载入中...
搜索中...
未找到
test_listview.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "test.h"
8
9#include <carla/ListView.h>
10
11#include <array>
12#include <cstring>
13#include <list>
14#include <set>
15#include <string>
16#include <vector>
17
18using carla::MakeListView;// 使用命名空间,简化对 Carla 中的 MakeListView 函数的调用
19
20template <typename Iterator>// 定义一个模板函数,用于测试不同迭代器类型的序列
22
23 int count = 0;
24 // 遍历 ListView 中的元素
25 for (auto &&x : view) {
26 // 断言当前元素与计数器的值相等
27 ASSERT_EQ(x, count);{
28
29 ++count;// 增加计数器的值
30 }
31
32 ASSERT_EQ(count, 6);//断言计数器的值为 6,确保 ListView 中有 6 个元素
33}
34TEST(listview, sequence) {
35 int array[] = {0, 1, 2, 3, 4, 5};
36
37 TestSequence(MakeListView(array));// 使用 MakeListView 创建一个 ListView 对象,并传入整数数组进行测试
38
39 std::array<int, 6u> std_array = {0, 1, 2, 3, 4, 5};
40
41 TestSequence(MakeListView(std_array)); // 使用 MakeListView 创建一个 ListView 对象,并传入 std::array 容器进行测试
42
43 std::vector<int> vector = {0, 1, 2, 3, 4, 5};
44
45 TestSequence(MakeListView(vector));// 使用 MakeListView 创建一个 ListView 对象,并传入 std::vector 容器进行测试
46 std::list<int> list = {0, 1, 2, 3, 4, 5};//清晰地创建了一个 std::list<int> 类型的列表(list),并初始化为包含从 0 到 5 的六个整数。
47 TestSequence(MakeListView(list));
48 std::set<int> set = {0, 1, 2, 3, 4, 5};//创建了一个包含六个不重复整数的集合
49 TestSequence(MakeListView(set));//转换集合为特定视图或可迭代对象的函数或模板
50}
51TEST(listview, string) {
52 std::string str = "Hello list view!";//范围for循环遍历一个由MakeListView(str)返回的对象
53
54 std::string result;
55
56
57 for (char c : MakeListView(str)) {
58 result += c;
59 }
60 // 断言结果字符串与原始字符串相等
61 ASSERT_EQ(result, str);
62 char hello[6u] = {0};
63 auto begin = std::begin(hello);
64 // 遍历由字符串的一部分创建的 ListView 对象,将字符逐个复制到字符数组中
65 for (char c : MakeListView(str.begin(), str.begin() + 5u)) {
66 *begin = c;
67
68 ++begin;
69 }
70 ASSERT_EQ(std::strcmp(hello, "Hello"), 0); // 断言字符数组中的内容与 "Hello" 相等
71}
auto begin() const noexcept
名称范围迭代支持
代表容器中一段元素的视图,基本上是一对起始和结束迭代器。
static auto MakeListView(Iterator begin, Iterator end)
TEST(image, support)
static void TestSequence(carla::ListView< Iterator > view)