Skip to content

Commit

Permalink
Миронов Илья - Лабораторная работа #2 (#395)
Browse files Browse the repository at this point in the history
* lab2

* Update test_zhatkin_v_levenshtein_distance.cpp

* cpplint

* cpplint

* cpplint

* cpplint

* cpplint

* cpplint

* Update modules/mironov_i_stats/include/stats.h

Co-authored-by: Kuznetsov-Artyom <[email protected]>

* Update modules/mironov_i_stats/src/stats.cpp

Co-authored-by: Kuznetsov-Artyom <[email protected]>

---------

Co-authored-by: Kuznetsov-Artyom <[email protected]>
  • Loading branch information
Hugelka and Kuznetsov-Artyom authored Oct 29, 2024
1 parent 4e54692 commit 8288f1e
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/Vasilev_i_lab2/src/Combinations_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include "include/Combinations.h"

Expand Down
3 changes: 3 additions & 0 deletions modules/Vasilev_i_lab2/test/test_Combinations.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2024 Vasilev Ivan
// Copyright 2024 Tushentsova Karina

#include <vector>

#include "gtest/gtest.h"

#include "include/Combinations.h"

TEST(CombinationGeneratorTest, BasicTest) {
Expand Down
12 changes: 12 additions & 0 deletions modules/mironov_i_stats/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Declare variables for binaries' names
get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
set(MODULE "${DIR_NAME}")
set(LIBRARY "lib_${MODULE}")
set(TESTS "test_${MODULE}")

# Include directory with public headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Add all submodules
add_subdirectory(src)
add_subdirectory(test)
12 changes: 12 additions & 0 deletions modules/mironov_i_stats/include/stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 Mironov Ilya

#pragma once

#include <vector>

namespace stats {
float moment(const std::vector<float>& data, size_t k, float mu);
float mean(const std::vector<float>& data);
float var_base(const std::vector<float>& data);
float var(const std::vector<float>& data);
} // namespace stats
18 changes: 18 additions & 0 deletions modules/mironov_i_stats/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set(target ${LIBRARY})

file(GLOB srcs "*.cpp")
file(GLOB hdrs "../include/*.h")
set_source_files_properties(${srcs} ${hdrs} PROPERTIES
LABELS "${MODULE};Library")

add_library(${target} STATIC ${srcs} ${hdrs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Library")

if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
target_link_libraries(${target} ${LIBRARY_DEPS})

set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE)
33 changes: 33 additions & 0 deletions modules/mironov_i_stats/src/stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Mironov Ilya

#include "include/stats.h"

#include <cmath>
#include <numeric>

namespace stats {

float moment(const std::vector<float>& data, size_t k, float mu) {
float result = 0.0f;
for (auto& x : data) {
result += std::pow(x - mu, k);
}
return result / data.size();
}
float mean(const std::vector<float>& data) {
return moment(data, 1, 0.0f);
}
float var_base(const std::vector<float>& data) {
return moment(data, 2, mean(data));
}
float var(const std::vector<float>& data) {
float result1 = 0.0f;
float result2 = 0.0f;
const auto size = data.size();
for (auto& x : data) {
result1 += x * x;
result2 += x;
}
return result1 / size - (result2 / size) * (result2 / size);
}
} // namespace stats
27 changes: 27 additions & 0 deletions modules/mironov_i_stats/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set(target ${TESTS})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Test")

add_executable(${target} ${srcs} ${hdrs})
set_target_properties(${target} PROPERTIES
LABELS "${MODULE};Test")

if (UNIX)
target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread)
endif (UNIX)
target_link_libraries(${target} gtest ${LIBRARY})

# VS2012 doesn't support correctly the tuples yet,
# see http://code.google.com/p/googletest/issues/detail?id=412
if(MSVC)
target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10)
endif()

add_test(
NAME ${MODULE}_gtest
COMMAND ${target}
)
set_tests_properties (${MODULE}_gtest PROPERTIES
LABELS "${MODULE}")
8 changes: 8 additions & 0 deletions modules/mironov_i_stats/test/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 Mironov Ilya

#include <gtest/gtest.h>

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
43 changes: 43 additions & 0 deletions modules/mironov_i_stats/test/test_stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 Mironov Ilya

#include <gtest/gtest.h>

#include <numeric>

#include "include/stats.h"

TEST(TestStats, OneElementMean) {
std::vector<float> data = {1.0f};
EXPECT_FLOAT_EQ(1.0f, stats::moment(data, 1, 0.0f));
}

TEST(TestStats, OneElementVar) {
std::vector<float> data = {1.0f};
const float mean = stats::moment(data, 1, 0.0f);
EXPECT_FLOAT_EQ(0.0f, stats::moment(data, 2, mean));
}

TEST(TestStats, CompareVarImplsSimple) {
std::vector<float> data = {1.0f, 2.0f, 3.0f};
EXPECT_NEAR(0.6666f, stats::var(data), 1e-4);
EXPECT_NEAR(0.6666f, stats::var_base(data), 1e-4);
}

TEST(TestStats, SimpleData) {
std::vector<float> data = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
EXPECT_FLOAT_EQ(3.0f, stats::mean(data));
EXPECT_FLOAT_EQ(2.0f, stats::var(data));
EXPECT_FLOAT_EQ(2.0f, stats::var_base(data));
}

TEST(TestStats, CompareVarImplsIota) {
std::vector<float> data(100);
std::iota(data.begin(), data.end(), 0.0f);
ASSERT_FLOAT_EQ(stats::var_base(data), stats::var(data));
}

TEST(TestStats, NegativeSimpleData) {
std::vector<float> data = {-1.0f, -2.0f, -3.0f, -4.0f, -5.0f};
EXPECT_FLOAT_EQ(-3.0f, stats::mean(data));
EXPECT_FLOAT_EQ(2.0f, stats::var(data));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <gtest/gtest.h>


#include "include/levenshtein_distance.h"
TEST(zhatkin_v_levenshtein_distance, constructors) {
LevenshteinDistance ld1;
Expand Down

0 comments on commit 8288f1e

Please sign in to comment.