Skip to content

Commit a456c78

Browse files
committed
task 13.01
1 parent 063e7fb commit a456c78

File tree

7 files changed

+80
-3
lines changed

7 files changed

+80
-3
lines changed

week-16/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(DIRS task-13-02)
1+
set(DIRS task-13-01 task-13-02)
22
foreach(DIR ${DIRS})
33
add_subdirectory(${DIR})
44
endforeach()

week-16/task-13-01/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(include)
2+
3+
add_subdirectory(test)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <iomanip>
5+
#include <sstream>
6+
#include <string>
7+
#include <vector>
8+
9+
std::string bytes_to_hex_string(const std::vector<uint8_t>& bytes) {
10+
std::ostringstream oss;
11+
oss << std::hex << std::uppercase << std::setfill('0');
12+
for (uint8_t const byte : bytes) {
13+
oss << std::setw(2) << static_cast<int>(byte);
14+
}
15+
return oss.str();
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_test(test-13-01.cpp)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <cstdint>
2+
#include <vector>
3+
4+
#include "bytes-to-hex.hpp"
5+
#include "gtest/gtest.h"
6+
7+
TEST(BytesToHexTest, EmptyVector) {
8+
const std::vector<uint8_t> input;
9+
EXPECT_EQ(bytes_to_hex_string(input), "");
10+
}
11+
12+
TEST(BytesToHexTest, SingleByte) {
13+
EXPECT_EQ(bytes_to_hex_string({0x00}), "00");
14+
EXPECT_EQ(bytes_to_hex_string({0xFF}), "FF");
15+
EXPECT_EQ(bytes_to_hex_string({0x0A}), "0A");
16+
EXPECT_EQ(bytes_to_hex_string({0xA0}), "A0");
17+
}
18+
19+
TEST(BytesToHexTest, CommonCases) {
20+
EXPECT_EQ(bytes_to_hex_string({0xBA, 0xAD}), "BAAD");
21+
EXPECT_EQ(bytes_to_hex_string({0xDE, 0xAD, 0xBE, 0xEF}), "DEADBEEF");
22+
EXPECT_EQ(bytes_to_hex_string({0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}),
23+
"0123456789ABCDEF");
24+
}
25+
26+
TEST(BytesToHexTest, AllPossibleBytes) {
27+
std::vector<uint8_t> all_bytes(256);
28+
for (int i = 0; i < 256; ++i) {
29+
all_bytes[i] = static_cast<uint8_t>(i);
30+
}
31+
const std::string result = bytes_to_hex_string(all_bytes);
32+
ASSERT_EQ(result.size(), 512); // 256 bytes * 2 chars
33+
for (int i = 0; i < 256; ++i) {
34+
const std::string byte_str = result.substr(i * 2, 2);
35+
const std::string expected = std::string(1, "0123456789ABCDEF"[i >> 4]) +
36+
std::string(1, "0123456789ABCDEF"[i & 0x0F]);
37+
EXPECT_EQ(byte_str, expected);
38+
}
39+
}
40+
41+
TEST(BytesToHexTest, CorrectFormatting) {
42+
const std::vector<uint8_t> input = {0x1, 0x2, 0x03, 0x10, 0xFF};
43+
const std::string result = bytes_to_hex_string(input);
44+
EXPECT_EQ(result, "01020310FF");
45+
EXPECT_TRUE(result.find("01") != std::string::npos);
46+
EXPECT_TRUE(result.find("02") != std::string::npos);
47+
}
48+
49+
TEST(BytesToHexTest, LargeInput) {
50+
const std::vector<uint8_t> large_input(10000, 0xAB);
51+
const std::string result = bytes_to_hex_string(large_input);
52+
EXPECT_EQ(result.size(), 20000);
53+
for (size_t i = 0; i < result.size(); i += 2) {
54+
EXPECT_EQ(result.substr(i, 2), "AB");
55+
}
56+
}

week-16/task-13-02/include/pascal-triangle.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <iomanip>
4-
#include <iostream>
54

65
class PascalsTriangle {
76
public:

week-9/task-06-03/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
add_library(entity)
1+
add_library(entity
2+
../../week-16/task-13-01/include/bytes-to-hex.hpp
3+
../../week-16/task-13-01/test/test-13-01.cpp)
24
target_sources(entity
35
PUBLIC
46
FILE_SET CXX_MODULES FILES

0 commit comments

Comments
 (0)