当前位置:网站首页>面试题-给::memcpy函数设计测试用例
面试题-给::memcpy函数设计测试用例
2022-07-17 10:18:00 【zhuge19870104】
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(memcpy_test)
set(CMAKE_CXX_STANDARD 20)
add_definitions(-g)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../include)
string(REPLACE ".cpp" "" file "main.cpp")
add_executable(${file} "main.cpp")
main.cpp
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
static int const MAX_LEN = 128;
static int const SRC_LEN = 6;
struct CopyItem {
char* src;
char* dst;
int src_offset;
int dst_offset;
int copy_len;
bool is_source{false};
};
void recover_source_and_dst(char* src, char* dst) {
for (int i = 0; i < SRC_LEN; ++i) {
if (i == SRC_LEN - 1) {
src[i] = '\0';
} else {
src[i] = 'a' + i;
}
}
memset(dst, 0, sizeof(dst));
}
int get_copy_len(CopyItem const& test_case) {
auto copy_l = test_case.copy_len;
auto copy_len = copy_l == -1 ? strlen(test_case.src) + 1 : copy_l;
return copy_len;
}
int main(int argc, char* argv[]) {
char src[SRC_LEN] = "abcde";
char dst[MAX_LEN] = {0};
vector<CopyItem> positive_test_cases{
/* Case 1: src里面的所有字符拷贝到 dst,一把梭*/
CopyItem{src, dst, 0, 0, -1, false},
/* Case 2: src里面的前三个字符拷贝回src, 还是abcde, 不会出现内存错误 */
CopyItem{src, src, 0, 0, 3, true},
/* Case 3: src从第二个字符开始,拷贝三个字符到src,输出结果 cdede,不会出现内存错误 */
CopyItem{src, src, 2, 0, 3, true},
/* Case 4: src拷一部分到dst, 应该没问题 */
CopyItem{src, dst, 2, 0, 3, false}
};
for (auto const& test_case : positive_test_cases) {
auto copy_len = get_copy_len(test_case);
memcpy((char*)test_case.dst + test_case.dst_offset,
(char*)test_case.src + test_case.src_offset, copy_len);
char* res = test_case.is_source ? test_case.src : test_case.dst;
cout << "Dest: " << res << endl;
recover_source_and_dst(src, dst);
}
vector<CopyItem> negative_test_cases{
/* Case 5: 把src拷贝到空串,没有足够存储空间,看能不能拷,这里应该崩 */
CopyItem{src, (char*)"", 0, 0, -1, false},
/* Case 6: 把src拷贝到nullptr, 空指针, 看能不能拷, 这里应该崩 */
CopyItem{src, nullptr, 0, 0, -1, false}
};
for (auto const& test_case : negative_test_cases) {
auto copy_len = get_copy_len(test_case);
memcpy((char*)test_case.dst + test_case.dst_offset,
(char*)test_case.src + test_case.src_offset, copy_len);
char* res = test_case.is_source ? test_case.src : test_case.dst;
cout << "Dest: " << res << endl;
}
return EXIT_SUCCESS;
}
用例输出的结果如下,
边栏推荐
猜你喜欢

Daily model series: July 11, 2022

Anycontrol demo demo demo

How is MySQL data stored on disk?

【微服务~高级】配置中心实战

Un7.16: how to deploy projects on code cloud and invite team members?

Set the ID field to increase automatically when creating tables in SQL Server (Navicat demo)

每日模型系列:2022.07.11

Solve interface cross domain problems and node operation MySQL

Day 6 training

LDA classifier
随机推荐
07---布儒斯特角
封装API,request拦截,response拦截,身份验证时效性
Towhee daily model weekly report
Flink小知识--任务调度slot的配置 slotSharingGroup
简易的第三方组件日志脱敏
Microservice splitting for stand-alone projects
案例分享 | 基于Linkis+DSS构建合合信息一站式数据开发平台
置顶随笔分类
Qprocess of QT
第六天训练
cookie和session在实际项目中的使用
代码随想录:刷题记录(更新中)
Uniapp warehouse management system source code
Code Capriccio: question skimming record (under update)
Project contract analysis from the perspective of Software Engineer
2、 Pinda general permission system__ Project construction
L2-029 特立独行的幸福
Anycontrol demo demo demo
ETCD数据库源码分析——初始化EtcdServer结构体
研究发现DNA纳米设备注射液可安全用于医疗用途