Created
April 27, 2023 20:02
-
-
Save Eraden/b028190b3e12a555fb23d196b7b1b712 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.24) | |
project(studia) | |
set(CMAKE_CXX_STANDARD 23) | |
find_package(GTest REQUIRED) | |
enable_testing() | |
include_directories(${GTEST_INCLUDE_DIRS}) | |
add_executable(studia main.cpp) | |
add_executable(unit_tests test.cpp) | |
target_link_libraries(unit_tests gtest gtest_main) | |
add_test(AllTests unit_tests) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <vector> | |
#include <set> | |
#include <algorithm> | |
#include <string> | |
using namespace std; | |
void Add(const int n, vector<int> &out) { | |
if (n < 1 || n > 4095) { | |
return; | |
} | |
out.push_back(n); | |
} | |
void Create(const set<int> a, const unsigned len, vector<int> &coll) { | |
int idx = 0; | |
for (int n : a) { | |
if (idx >= len) { | |
break; | |
} | |
coll.push_back(n); | |
idx++; | |
} | |
} | |
void Complement(set<int> const &refilled, vector<int> &complementary) { | |
int min = 0, max = 0; | |
for (int n : refilled) { | |
if (min == 0 || min > n) { | |
min = n; | |
} | |
if (max == 0 || max < n) { | |
max = n; | |
} | |
} | |
for (int i = min; i <= max; i++) { | |
if (refilled.find(i) != refilled.end()) { | |
continue; | |
} | |
complementary.push_back(i); | |
} | |
} | |
void Union(set<int> const &a, set<int> const &b, vector<int> &out) { | |
for (int n : a) { | |
out.push_back(n); | |
} | |
for (int n : b) { | |
if (find(out.begin(), out.end(), n) != out.end()) { | |
continue; | |
} | |
out.push_back(n); | |
} | |
} | |
void Intersection(set<int> const &a, set<int> const &b, vector<int> &out) { | |
for (int n : a) { | |
if (find(b.begin(), b.end(), n) != b.end()) { | |
out.push_back(n); | |
} | |
} | |
} | |
void Difference(set<int> const &subtract, set<int> const &subtractor, vector<int> &out) { | |
for (int n : subtract) { | |
if (find(subtractor.begin(), subtractor.end(), n) != subtractor.end()) { | |
continue; | |
} | |
out.push_back(n); | |
} | |
} | |
void Symmetric(set<int> const &a, set<int> const &b, vector<int> &out) { | |
for (int n : a) { | |
if (find(b.begin(), b.end(), n) != b.end()) { | |
continue; | |
} | |
out.push_back(n); | |
} | |
for (int n : b) { | |
if (find(a.begin(), a.end(), n) != a.end()) { | |
continue; | |
} | |
out.push_back(n); | |
} | |
} | |
bool Subset(set<int> const &included, set<int> const &including) { | |
if (included.size() > including.size() || included.empty()) { | |
return false; | |
} | |
for (int n : included) { | |
if (find(including.begin(), including.end(), n) == including.end()) { | |
return false; | |
} | |
} | |
return true; | |
} | |
bool Equal(set<int> const &a, set<int> const &b) { | |
if (a.size() != b.size()) { | |
return false; | |
} | |
if (a.empty() && b.empty()) { | |
return true; | |
} | |
std::set<int>::iterator ait = a.begin(); | |
std::set<int>::iterator bit = b.begin(); | |
for (int i = 0; i < a.size(); i++) { | |
if (*ait != *bit) { | |
return false; | |
} | |
ait++; | |
bit++; | |
} | |
return true; | |
} | |
bool Empty(set<int> const &a) { | |
return a.empty(); | |
} | |
bool Notempty(set<int> const &a) { | |
return !a.empty(); | |
} | |
bool Element(int n, set<int> const &a) { | |
for (int el : a) { | |
if (el == n) { | |
return true; | |
} | |
} | |
return false; | |
} | |
double Arithmetic(set<int> const &a) { | |
int n = 0; | |
if (a.empty()) { | |
return 0; | |
} | |
for (int el : a) { | |
n += el; | |
} | |
return (double) n / (double) a.size(); | |
} | |
double Harmonic(set<int> const &a) { | |
double sum = 0.0; | |
if (a.empty()) { | |
return 1.0; | |
} | |
for (int n : a) { | |
sum += 1.0 / (double) n; | |
} | |
return ((double) a.size() / sum); | |
} | |
void MinMax(set<int> const &a, int& min, int& max) { | |
for (int n : a) { | |
if (min == 0 || min > n) { | |
min = n; | |
} | |
if (max == 0 || max < n) { | |
max = n; | |
} | |
} | |
} | |
void Cardinality(set<int> const &a, int& p) { | |
p = a.size(); | |
} | |
void Properties(set<int> const &a, const std::string& op, double& arithmetic, double& harmonic, int &min, int& max, int& p) { | |
for (char c : op) { | |
switch (c) { | |
case 'a': { | |
arithmetic = Arithmetic(a); | |
break; | |
} | |
case 'h': { | |
harmonic = Harmonic(a); | |
break; | |
} | |
case 'm': { | |
MinMax(a, min, max); | |
break; | |
} | |
case 'c': { | |
Cardinality(a, p); | |
break; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "impl.hpp" | |
#include <gtest/gtest.h> | |
#include <numeric> | |
#include <limits> | |
#include <cmath> | |
#include <set> | |
#include <string> | |
TEST(AddUsage, AddInRange) { | |
vector<int> coll; | |
Add(1, coll); | |
Add(7, coll); | |
Add(4095, coll); | |
EXPECT_EQ(coll[0], 1); | |
EXPECT_EQ(coll[1], 7); | |
EXPECT_EQ(coll[2], 4095); | |
} | |
TEST(AddUsage, AddOutsideRange) { | |
vector<int> coll; | |
Add(-1, coll); | |
Add(0, coll); | |
Add(4096, coll); | |
Add(9000, coll); | |
EXPECT_EQ(coll.size(), 0); | |
} | |
TEST(CreateUsage, Simple) { | |
vector<int> coll; | |
set<int> a = {1,2,3,4,5}; | |
Create(a, 5, coll); | |
EXPECT_EQ(coll[0], 1); | |
EXPECT_EQ(coll[1], 2); | |
EXPECT_EQ(coll[2], 3); | |
EXPECT_EQ(coll[3], 4); | |
EXPECT_EQ(coll[4], 5); | |
} | |
TEST(ComplementUsage, SmallRange) { | |
set<int> coll = {1, 4, 5, 7}; | |
vector<int> out; | |
Complement(coll, out); | |
EXPECT_EQ(out[0], 2); | |
EXPECT_EQ(out[1], 3); | |
EXPECT_EQ(out[2], 6); | |
} | |
TEST(ComplementUsage, LargeRange) { | |
set<int> coll = {1, 4, 5, 7, 30}; | |
vector<int> out; | |
Complement(coll, out); | |
EXPECT_EQ(out[0], 2); | |
EXPECT_EQ(out[1], 3); | |
EXPECT_EQ(out[2], 6); | |
EXPECT_EQ(out[3], 8); | |
EXPECT_EQ(out[4], 9); | |
EXPECT_EQ(out[5], 10); | |
} | |
TEST(UnionUsage, SmallRange) { | |
set<int> a = {1, 4, 5, 7, 30}; | |
set<int> b = {3, 4, 6, 7, 10}; | |
vector<int> out; | |
Union(a, b, out); | |
vector<int> expected = {1,4,5,7,30,3,6,10}; | |
EXPECT_EQ(out, expected); | |
} | |
TEST(IntersectionUsage, SmallRange) { | |
set<int> a = {1, 4, 5, 7, 30}; | |
set<int> b = {3, 4, 6, 7, 10}; | |
vector<int> out; | |
Intersection(a, b, out); | |
vector<int> expected = { 4, 7 }; | |
EXPECT_EQ(out, expected); | |
} | |
TEST(DifferenceUsage, SmallRange) { | |
set<int> a = {1, 2, 3, 4, 5}; | |
set<int> b = {3, 4, 5, 6, 7}; | |
vector<int> out; | |
Difference(a, b, out); | |
vector<int> expected = { 1, 2 }; | |
EXPECT_EQ(out, expected); | |
} | |
TEST(SymmetricUsage, SmallRange) { | |
set<int> a = {1, 2, 3}; | |
set<int> b = {2, 3, 4}; | |
vector<int> out; | |
Symmetric(a, b, out); | |
vector<int> expected = { 1, 4 }; | |
EXPECT_EQ(out, expected); | |
} | |
TEST(SubsetUsage, Positive) { | |
set<int> included = {1, 2}; | |
set<int> including = {1, 2, 3, 4}; | |
bool out = Subset(included, including); | |
EXPECT_EQ(out, true); | |
} | |
TEST(SubsetUsage, Netative) { | |
set<int> including = {1, 2}; | |
set<int> included = {1, 2, 3, 4, 5}; | |
bool out = Subset(included, including); | |
EXPECT_EQ(out, false); | |
} | |
TEST(EqualUsage, Positive) { | |
set<int> a = {1, 2, 3, 4}; | |
set<int> b = {1, 2, 3, 4}; | |
bool out = Equal(a, b); | |
EXPECT_EQ(out, true); | |
} | |
TEST(EqualUsage, Netative) { | |
set<int> a = {1, 2}; | |
set<int> b = {1, 2, 3, 4, 5}; | |
bool out = Equal(a, b); | |
EXPECT_EQ(out, false); | |
} | |
TEST(EmptyUsage, Positive) { | |
set<int> a = {}; | |
bool out = Empty(a); | |
EXPECT_EQ(out, true); | |
} | |
TEST(EmptyUsage, Netative) { | |
set<int> a = {1, 2}; | |
bool out = Empty(a); | |
EXPECT_EQ(out, false); | |
} | |
TEST(NotEmptyUsage, Positive) { | |
set<int> a = {1,2}; | |
bool out = Notempty(a); | |
EXPECT_EQ(out, true); | |
} | |
TEST(NotEmptyUsage, Netative) { | |
set<int> a = {}; | |
bool out = Notempty(a); | |
EXPECT_EQ(out, false); | |
} | |
TEST(ElementUsage, Positive) { | |
set<int> a = {1,2,3,4,5}; | |
bool out = Element(3, a); | |
EXPECT_EQ(out, true); | |
} | |
TEST(ElementUsage, Netative) { | |
set<int> a = {1, 2}; | |
bool out = Element(5, a); | |
EXPECT_EQ(out, false); | |
} | |
TEST(ArithmeticUsage, Empty) { | |
set<int> a = {}; | |
double out = Arithmetic(a); | |
EXPECT_EQ(out, 0.0); | |
} | |
TEST(ArithmeticUsage, Simple) { | |
set<int> a = {1, 2}; | |
double out = Arithmetic(a); | |
EXPECT_EQ(out, 1.5); | |
} | |
TEST(ArithmeticUsage, Larger) { | |
set<int> a = {1, 2, 3}; | |
double out = Arithmetic(a); | |
EXPECT_EQ(out, 2.0); | |
} | |
TEST(HarmonicUsage, Simple) { | |
set<int> a = {1,2,4,6}; | |
double out = Harmonic(a); | |
double expected = 2.087; | |
EXPECT_TRUE(std::fabs(out - expected) > std::numeric_limits<double>::epsilon()); | |
} | |
TEST(MinMaxUsage, Simple) { | |
set<int> a = {7, 2, 4, 6, 20, 9}; | |
int min = 4096, max = 0; | |
MinMax(a, min, max); | |
EXPECT_EQ(min, 2); | |
EXPECT_EQ(max, 20); | |
} | |
TEST(MinMaxUsage, Empty) { | |
set<int> a = {}; | |
int min = 4096, max = 0; | |
MinMax(a, min, max); | |
EXPECT_EQ(min, 4096); | |
EXPECT_EQ(max, 0); | |
} | |
TEST(CardinalityUsage, Empty) { | |
set<int> a = {}; | |
int p = 0; | |
Cardinality(a, p); | |
EXPECT_EQ(p, 0); | |
} | |
TEST(CardinalityUsage, NonEmpty) { | |
set<int> a = {1,4,5,79,9,3}; | |
int p = 0; | |
Cardinality(a, p); | |
EXPECT_EQ(p, 6); | |
} | |
TEST(PropertiesUsage, NoOp) { | |
double arithmetic = 0, harmonic = 0; | |
int min = 0, max = 0, p = 0; | |
std::string op(""); | |
set<int> a = {1,4,5,79,9,3}; | |
Properties(a, op, arithmetic, harmonic, min, max, p); | |
EXPECT_EQ(arithmetic, 0.0); | |
EXPECT_EQ(harmonic, 0.0); | |
EXPECT_EQ(min, 0); | |
EXPECT_EQ(max, 0); | |
EXPECT_EQ(p, 0); | |
} | |
TEST(PropertiesUsage, AllOp) { | |
double arithmetic = 0, harmonic = 0; | |
int min = 0, max = 0, p = 0; | |
std::string op("ahmc"); | |
set<int> a = {1,4,5,79,9,3}; | |
Properties(a, op, arithmetic, harmonic, min, max, p); | |
EXPECT_TRUE(std::fabs(arithmetic - 16.833) > std::numeric_limits<double>::epsilon()); | |
EXPECT_TRUE(std::fabs(harmonic - 3.146) > std::numeric_limits<double>::epsilon()); | |
EXPECT_EQ(min, 1); | |
EXPECT_EQ(max, 79); | |
EXPECT_EQ(p, 6); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment