Skip to content

Задание 14.04.25 : 13.01 - 13.11 / 150 баллов / 20.04.25 23:59:59 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Checks: '
-readability-identifier-length,
-readability-else-after-return,
-readability-qualified-auto,
-readability-math-missing-parentheses
-readability-math-missing-parentheses,
-readability-magic-numbers
'
CheckOptions:
- key: readability-identifier-naming.NamespaceCase
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ include_directories(shared)


##### subdirectories #####
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11)
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11 week-16)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
Expand Down
4 changes: 4 additions & 0 deletions week-16/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(DIRS task-13-01 task-13-02)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
3 changes: 3 additions & 0 deletions week-16/task-13-01/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
16 changes: 16 additions & 0 deletions week-16/task-13-01/include/bytes-to-hex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <cstdint>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>

std::string bytes_to_hex_string(const std::vector<uint8_t>& bytes) {
std::ostringstream oss;
oss << std::hex << std::uppercase << std::setfill('0');
for (uint8_t const byte : bytes) {
oss << std::setw(2) << static_cast<int>(byte);
}
return oss.str();
}
1 change: 1 addition & 0 deletions week-16/task-13-01/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-13-01.cpp)
56 changes: 56 additions & 0 deletions week-16/task-13-01/test/test-13-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <cstdint>
#include <vector>

#include "bytes-to-hex.hpp"
#include "gtest/gtest.h"

TEST(BytesToHexTest, EmptyVector) {
const std::vector<uint8_t> input;
EXPECT_EQ(bytes_to_hex_string(input), "");
}

TEST(BytesToHexTest, SingleByte) {
EXPECT_EQ(bytes_to_hex_string({0x00}), "00");
EXPECT_EQ(bytes_to_hex_string({0xFF}), "FF");
EXPECT_EQ(bytes_to_hex_string({0x0A}), "0A");
EXPECT_EQ(bytes_to_hex_string({0xA0}), "A0");
}

TEST(BytesToHexTest, CommonCases) {
EXPECT_EQ(bytes_to_hex_string({0xBA, 0xAD}), "BAAD");
EXPECT_EQ(bytes_to_hex_string({0xDE, 0xAD, 0xBE, 0xEF}), "DEADBEEF");
EXPECT_EQ(bytes_to_hex_string({0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}),
"0123456789ABCDEF");
}

TEST(BytesToHexTest, AllPossibleBytes) {
std::vector<uint8_t> all_bytes(256);
for (int i = 0; i < 256; ++i) {
all_bytes[i] = static_cast<uint8_t>(i);
}
const std::string result = bytes_to_hex_string(all_bytes);
ASSERT_EQ(result.size(), 512); // 256 bytes * 2 chars
for (int i = 0; i < 256; ++i) {
const std::string byte_str = result.substr(i * 2, 2);
const std::string expected = std::string(1, "0123456789ABCDEF"[i >> 4]) +
std::string(1, "0123456789ABCDEF"[i & 0x0F]);
EXPECT_EQ(byte_str, expected);
}
}

TEST(BytesToHexTest, CorrectFormatting) {
const std::vector<uint8_t> input = {0x1, 0x2, 0x03, 0x10, 0xFF};
const std::string result = bytes_to_hex_string(input);
EXPECT_EQ(result, "01020310FF");
EXPECT_TRUE(result.find("01") != std::string::npos);
EXPECT_TRUE(result.find("02") != std::string::npos);
}

TEST(BytesToHexTest, LargeInput) {
const std::vector<uint8_t> large_input(10000, 0xAB);
const std::string result = bytes_to_hex_string(large_input);
EXPECT_EQ(result.size(), 20000);
for (size_t i = 0; i < result.size(); i += 2) {
EXPECT_EQ(result.substr(i, 2), "AB");
}
}
3 changes: 3 additions & 0 deletions week-16/task-13-02/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
54 changes: 54 additions & 0 deletions week-16/task-13-02/include/pascal-triangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <iomanip>

class PascalsTriangle {
public:
explicit PascalsTriangle(size_t n) : n_(n) {}

void print(std::ostream& out) {
if (n_ == 0) {
return;
}
if (!calculated_) {
triangle_ = calculate(n_);
calculated_ = true;
}
const size_t max_width = num_digits(max_value(triangle_));
for (int i = 0; i < n_; ++i) {
out << std::string((n_ - i - 1) * (max_width + 1) / 2, ' ');
for (const int num : triangle_[i]) {
out << std::setw(max_width) << num << ' ';
}
out << '\n';
}
}

private:
static std::vector<std::vector<int>> calculate(size_t n) {
std::vector<std::vector<int>> triangle(n);
for (int i = 0; i < n; ++i) {
triangle[i].resize(i + 1);
triangle[i][0] = triangle[i][i] = 1;
for (int j = 1; j < i; ++j) {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}
return triangle;
}
static int max_value(const std::vector<std::vector<int>>& triangle) {
return triangle.back()[triangle.back().size() / 2];
}
static size_t num_digits(int num) {
size_t res = 0;
while (num > 0) {
num /= 10;
res += 1;
}
return res;
}

size_t n_;
bool calculated_ = false;
std::vector<std::vector<int>> triangle_;
};
1 change: 1 addition & 0 deletions week-16/task-13-02/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-13-02.cpp)
46 changes: 46 additions & 0 deletions week-16/task-13-02/test/test-13-02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "gtest/gtest.h"
#include "pascal-triangle.hpp"

class PascalsTriangleTest : public ::testing::Test {
protected:
static std::string captureOutput(int rows) {
std::stringstream ss;
PascalsTriangle triangle(rows);
triangle.print(ss);
return ss.str();
}
};

TEST_F(PascalsTriangleTest, HandlesZeroRows) {
PascalsTriangle triangle(0);
std::stringstream ss;
triangle.print(ss);
EXPECT_TRUE(ss.str().empty());
}

TEST_F(PascalsTriangleTest, HandlesSingleRow) {
auto output = captureOutput(1);
EXPECT_EQ(output, "1 \n");
}

TEST_F(PascalsTriangleTest, HandlesMultipleRows) {
auto output = captureOutput(3);
const std::string expected =
" 1 \n"
" 1 1 \n"
"1 2 1 \n";
EXPECT_EQ(output, expected);
}

TEST_F(PascalsTriangleTest, OutputFormatting) {
auto output = captureOutput(5); // NOLINT
const std::string expected =
" 1 \n"
" 1 1 \n"
" 1 2 1 \n"
" 1 3 3 1 \n"
"1 4 6 4 1 \n";
EXPECT_EQ(output, expected);
}

TEST_F(PascalsTriangleTest, JustPrintATriangle) { PascalsTriangle(10).print(std::cout); }