Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
16 changes: 15 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
#include <iostream>

int main() { return 0; }
#include "topology_sort.hpp"

int main() {
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int m = 5;
std::optional<std::tuple<int*, int*>> b = SummanddsInArray(a, 9, m);
if (b) {
std::cout << "found" << std::endl;
std::cout << std::get<0>(b.value()) << " : " << *std::get<0>(b.value())
<< std::endl;
std::cout << std::get<1>(b.value()) << " : " << *std::get<1>(b.value())
<< std::endl;
} else
std::cout << "NOT found" << std::endl;
}
93 changes: 91 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,95 @@

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(SummanddsInArray, Simple1) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 9;
int m = 11;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_TRUE(a);
ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

имхо так лучше выглядит: ASSERT_EQ(*a->first + *a->second, m);

}

TEST(SummanddsInArray, Simple2) {
int arr[] = {1, 2, 3, 4, 5, 8, 9};
int n = 7;
int m = 5;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_TRUE(a);
ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m);
}
TEST(SummanddsInArray, Simple3) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 9;
int m = 3;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_TRUE(a);
ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m);
}

TEST(SummanddsInArray, Simple4) {
int arr[] = {1, 3, 5, 7, 9};
int n = 5;
int m = 7;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}

TEST(SummanddsInArray, Nullarr) {
int* arr = nullptr;
int n = 9;
int m = 3;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}

TEST(SummanddsInArray, Specific1) {
int arr_ref[] = {10, 2, 3, 4, 5, 5, 7, 8, 0};
int* arr = arr_ref + 1;
int n = 9;
int m = 5;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_TRUE(a);
ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m);
}

TEST(SummanddsInArray, Specific2) {
int arr[] = {1, 2, 4, 5, 8};
int n = 5;
int m = 8;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}

TEST(SummanddsInArray, Specific3) {
int arr[] = {1, 3, 4, 5, 8};
int n = 5;
int m = 16;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}

TEST(SummanddsInArray, Specific4) {
int arr[] = {1, 3, 4, 5, 5, 8};
int n = 6;
int m = 10;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_TRUE(a);
ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m);
}

TEST(SummanddsInArray, Specific5) {
int arr[] = {};
int n = 0;
int m = 10;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}

TEST(SummanddsInArray, Specific6) {
int arr[] = {10};
int n = 1;
int m = 20;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}
20 changes: 20 additions & 0 deletions task_01/src/topology_sort.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай файл переименуем

Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
#include "topology_sort.hpp"

#include <optional>

std::optional<std::tuple<int *, int *>> SummanddsInArray(int *arr, int n,
int sum) {
int *begin = arr;
int *end = arr + (n - 1);
while (begin && end && begin < end && *begin + *end != sum)
if ((*begin + *end) > sum)
end--;
else
begin++;
if ((!begin) || (!end) || (begin >= end) || (*begin + *end != sum))
begin = end = nullptr;

std::optional<std::tuple<int *, int *>> result = std::nullopt;
if (begin != nullptr && end != nullptr)
result = std::tuple<int *, int *>{begin, end};
return result;
}
10 changes: 9 additions & 1 deletion task_01/src/topology_sort.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#pragma once
#ifndef TOPOLOGY_SORT_HPP
#define TOPOLOGY_SORT_HPP

#include <optional>
#include <tuple>

std::optional<std::tuple<int*, int*>> SummanddsInArray(int* arr, int n, int m);

#endif