From 4cbf73f9202793ed2c14babf7628700f21da4d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=90=D0=B2=D0=B4?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2?= Date: Sun, 14 Dec 2025 20:50:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 12 +- CMakeLists.txt | 2 +- README.md | 2 +- include/AdditionalFunctions.h | 11 ++ include/tbitfield.h | 55 +++--- samples/sample_prime_numbers.cpp | 1 + sln/vc10/bitfield/bitfield.vcxproj | 5 +- sln/vc10/gtest/gtest.vcxproj | 5 +- .../sample_prime_numbers.vcxproj | 5 +- sln/vc10/test_bitfield/test_bitfield.vcxproj | 5 +- sln/vc9/bitfield.sln | 17 +- sln/vc9/bitfield/bitfield.vcxproj | 89 +++++++++ sln/vc9/bitfield/bitfield.vcxproj.filters | 29 +++ sln/vc9/gtest/gtest.vcxproj | 85 ++++++++ sln/vc9/gtest/gtest.vcxproj.filters | 23 +++ .../sample_prime_numbers.vcxproj | 109 +++++++++++ .../sample_prime_numbers.vcxproj.filters | 14 ++ sln/vc9/test_bitfield/test_bitfield.vcxproj | 116 +++++++++++ .../test_bitfield.vcxproj.filters | 20 ++ src/tbitfield.cpp | 183 +++++++++++++++--- src/tset.cpp | 106 ++++++++-- test/test_main.cpp | 7 +- test/test_tbitfield.cpp | 3 +- test/test_tset.cpp | 2 +- 24 files changed, 808 insertions(+), 98 deletions(-) create mode 100644 include/AdditionalFunctions.h create mode 100644 sln/vc9/bitfield/bitfield.vcxproj create mode 100644 sln/vc9/bitfield/bitfield.vcxproj.filters create mode 100644 sln/vc9/gtest/gtest.vcxproj create mode 100644 sln/vc9/gtest/gtest.vcxproj.filters create mode 100644 sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj create mode 100644 sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj.filters create mode 100644 sln/vc9/test_bitfield/test_bitfield.vcxproj create mode 100644 sln/vc9/test_bitfield/test_bitfield.vcxproj.filters diff --git a/.travis.yml b/.travis.yml index b3c28e1b1..da4e299cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,16 @@ language: cpp -compiler: - - gcc - - clang +matrix: + include: + - os: linux + compiler: gcc + - os: osx + compiler: clang before_script: - mkdir ../build - cd ../build script: - cmake ../mp2-lab1-set - make -j - - cd ./bin/ - - ./test_set --gtest_list_tests + - ./bin/test_set --gtest_list_tests notifications: email: false diff --git a/CMakeLists.txt b/CMakeLists.txt index 42127f033..b0c4fb869 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 2.8) set(PROJECT_NAME set) project(${PROJECT_NAME}) -# TODO(Kornyakov): not sure if these lines are needed +# TODO(Korniakov): not sure if these lines are needed set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) diff --git a/README.md b/README.md index 65bc10925..d9fc7fe1c 100644 --- a/README.md +++ b/README.md @@ -141,4 +141,4 @@ __Цель данной работы__ — разработка структу [youtube-playlist]: https://www.youtube.com/playlist?list=PLSzOhsr5tmhrgV7u7CSzX4Ki1a9r0AKzV [slides]: https://github.com/UNN-ITMM-Software/mp2-lab1-set/tree/master/docs/slides [upstream]: https://github.com/UNN-ITMM-Software/mp2-lab1-set -[gitter]: https://gitter.im/UNN-ITMM-Software/mp2-lab1-set +[gitter]: https://gitter.im/UNN-VMK-Software/mp2-lab1-set diff --git a/include/AdditionalFunctions.h b/include/AdditionalFunctions.h new file mode 100644 index 000000000..184b73757 --- /dev/null +++ b/include/AdditionalFunctions.h @@ -0,0 +1,11 @@ +// , , " -2", ++, +// + +#ifndef __ADDITIONALFUNCTIONS_H__ +#define __ADDITIONALFUNCTIONS__ + +#include + +size_t mylog2(size_t n); // https://stackoverflow.com/a/994647 + +#endif \ No newline at end of file diff --git a/include/tbitfield.h b/include/tbitfield.h index e5b17beae..1c5491d7d 100644 --- a/include/tbitfield.h +++ b/include/tbitfield.h @@ -20,31 +20,41 @@ class TBitField int BitLen; // длина битового поля - макс. к-во битов TELEM *pMem; // память для представления битового поля int MemLen; // к-во эл-тов Мем для представления бит.поля - + static size_t GET_INDEX_CONST; + static unsigned int mylog2(unsigned int val) { + if (val == 0) return UINT_MAX; + if (val == 1) return 0; + unsigned int ret = 0; + while (val > 1) { + val >>= 1; + ret++; + } + return ret; + } // методы реализации int GetMemIndex(const int n) const; // индекс в pМем для бита n (#О2) - TELEM GetMemMask (const int n) const; // битовая маска для бита n (#О3) + TELEM GetMemMask(const int n) const; // битовая маска для бита n (#О3) public: - TBitField(int len); // (#О1) - TBitField(const TBitField &bf); // (#П1) - ~TBitField(); // (#С) - - // доступ к битам - int GetLength(void) const; // получить длину (к-во битов) (#О) - void SetBit(const int n); // установить бит (#О4) - void ClrBit(const int n); // очистить бит (#П2) - int GetBit(const int n) const; // получить значение бита (#Л1) - - // битовые операции - int operator==(const TBitField &bf) const; // сравнение (#О5) - int operator!=(const TBitField &bf) const; // сравнение - TBitField& operator=(const TBitField &bf); // присваивание (#П3) - TBitField operator|(const TBitField &bf); // операция "или" (#О6) - TBitField operator&(const TBitField &bf); // операция "и" (#Л2) - TBitField operator~(void); // отрицание (#С) - - friend istream &operator>>(istream &istr, TBitField &bf); // (#О7) - friend ostream &operator<<(ostream &ostr, const TBitField &bf); // (#П4) + TBitField(int len); // (#О1) + TBitField(const TBitField& bf); // (#П1) + ~TBitField(); // (#С) + + // доступ к битам + int GetLength(void) const; // получить длину (к-во битов) (#О) + void SetBit(const int n); // установить бит (#О4) + void ClrBit(const int n); // очистить бит (#П2) + int GetBit(const int n) const; // получить значение бита (#Л1) + + // битовые операции + int operator==(const TBitField& bf) const; // сравнение (#О5) + int operator!=(const TBitField& bf) const; // сравнение + TBitField& operator=(const TBitField& bf); // присваивание (#П3) + TBitField operator|(const TBitField& bf); // операция "или" (#О6) + TBitField operator&(const TBitField& bf); // операция "и" (#Л2) + TBitField operator~(void); // отрицание (#С) + + friend istream& operator>>(istream& istr, TBitField& bf); // (#О7) + friend ostream& operator<<(ostream& ostr, const TBitField& bf); // (#П4) }; // Структура хранения битового поля // бит.поле - набор битов с номерами от 0 до BitLen @@ -53,3 +63,4 @@ class TBitField // О8 Л2 П4 С2 #endif + diff --git a/samples/sample_prime_numbers.cpp b/samples/sample_prime_numbers.cpp index 4cde7a162..b2ddb64ec 100644 --- a/samples/sample_prime_numbers.cpp +++ b/samples/sample_prime_numbers.cpp @@ -10,6 +10,7 @@ // #define USE_SET // Использовать класс TSet, // закоментировать, чтобы использовать битовое поле + #ifndef USE_SET // Использовать класс TBitField #include "tbitfield.h" diff --git a/sln/vc10/bitfield/bitfield.vcxproj b/sln/vc10/bitfield/bitfield.vcxproj index f4b10eb18..278fc137f 100644 --- a/sln/vc10/bitfield/bitfield.vcxproj +++ b/sln/vc10/bitfield/bitfield.vcxproj @@ -20,10 +20,12 @@ StaticLibrary Unicode true + v143 StaticLibrary Unicode + v143 @@ -85,6 +87,5 @@ - - + \ No newline at end of file diff --git a/sln/vc10/gtest/gtest.vcxproj b/sln/vc10/gtest/gtest.vcxproj index 518510409..9e4147a0d 100644 --- a/sln/vc10/gtest/gtest.vcxproj +++ b/sln/vc10/gtest/gtest.vcxproj @@ -20,10 +20,12 @@ StaticLibrary Unicode true + v143 StaticLibrary Unicode + v143 @@ -81,6 +83,5 @@ - - + \ No newline at end of file diff --git a/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj b/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj index 981eb64a1..6fd7c5aa0 100644 --- a/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj +++ b/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj @@ -20,10 +20,12 @@ Application Unicode true + v143 Application Unicode + v143 @@ -105,6 +107,5 @@ - - + \ No newline at end of file diff --git a/sln/vc10/test_bitfield/test_bitfield.vcxproj b/sln/vc10/test_bitfield/test_bitfield.vcxproj index 5b72614b8..99e88bc6e 100644 --- a/sln/vc10/test_bitfield/test_bitfield.vcxproj +++ b/sln/vc10/test_bitfield/test_bitfield.vcxproj @@ -20,10 +20,12 @@ Application Unicode true + v143 Application Unicode + v143 @@ -112,6 +114,5 @@ - - + \ No newline at end of file diff --git a/sln/vc9/bitfield.sln b/sln/vc9/bitfield.sln index 1c3860f54..8f3dfc6b1 100644 --- a/sln/vc9/bitfield.sln +++ b/sln/vc9/bitfield.sln @@ -1,13 +1,15 @@  -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gtest", "gtest\gtest.vcproj", "{60932AFC-7808-48B7-B3BF-F2BC151C0065}" +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 18 +VisualStudioVersion = 18.0.11222.15 d18.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gtest", "gtest\gtest.vcxproj", "{60932AFC-7808-48B7-B3BF-F2BC151C0065}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bitfield", "bitfield\bitfield.vcproj", "{A76282B3-4276-4945-9AF2-3AC68111A828}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bitfield", "bitfield\bitfield.vcxproj", "{A76282B3-4276-4945-9AF2-3AC68111A828}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_bitfield", "test_bitfield\test_bitfield.vcproj", "{C650C93E-F0A7-4235-9F5F-0DCE78609BFB}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_bitfield", "test_bitfield\test_bitfield.vcxproj", "{C650C93E-F0A7-4235-9F5F-0DCE78609BFB}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_prime_numbers", "sample_prime_numbers\sample_prime_numbers.vcproj", "{5D0218F2-E502-414F-BA66-9E76B2785178}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_prime_numbers", "sample_prime_numbers\sample_prime_numbers.vcxproj", "{5D0218F2-E502-414F-BA66-9E76B2785178}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -35,4 +37,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1971A87A-F536-4FA7-8CBA-F3B93566B01E} + EndGlobalSection EndGlobal diff --git a/sln/vc9/bitfield/bitfield.vcxproj b/sln/vc9/bitfield/bitfield.vcxproj new file mode 100644 index 000000000..02db2252a --- /dev/null +++ b/sln/vc9/bitfield/bitfield.vcxproj @@ -0,0 +1,89 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + 18.0 + {A76282B3-4276-4945-9AF2-3AC68111A828} + bitfield + Win32Proj + + + + StaticLibrary + v145 + Unicode + true + + + StaticLibrary + v145 + Unicode + + + + + + + + + + + + + <_ProjectFileVersion>18.0.11123.170 + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + + Disabled + ../../../include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + Level3 + EditAndContinue + + + + + MaxSpeed + true + ../../../include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + Level3 + ProgramDatabase + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sln/vc9/bitfield/bitfield.vcxproj.filters b/sln/vc9/bitfield/bitfield.vcxproj.filters new file mode 100644 index 000000000..98c35097d --- /dev/null +++ b/sln/vc9/bitfield/bitfield.vcxproj.filters @@ -0,0 +1,29 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/sln/vc9/gtest/gtest.vcxproj b/sln/vc9/gtest/gtest.vcxproj new file mode 100644 index 000000000..f9232bd74 --- /dev/null +++ b/sln/vc9/gtest/gtest.vcxproj @@ -0,0 +1,85 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + 18.0 + {60932AFC-7808-48B7-B3BF-F2BC151C0065} + gtest + Win32Proj + + + + StaticLibrary + v145 + Unicode + true + + + StaticLibrary + v145 + Unicode + + + + + + + + + + + + + <_ProjectFileVersion>18.0.11123.170 + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;GTEST_EXPORTS;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + Level3 + EditAndContinue + + + + + MaxSpeed + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;GTEST_EXPORTS;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + Level3 + ProgramDatabase + + + + + + + + + + + + \ No newline at end of file diff --git a/sln/vc9/gtest/gtest.vcxproj.filters b/sln/vc9/gtest/gtest.vcxproj.filters new file mode 100644 index 000000000..dd2af2286 --- /dev/null +++ b/sln/vc9/gtest/gtest.vcxproj.filters @@ -0,0 +1,23 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj b/sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj new file mode 100644 index 000000000..6d78ba5fc --- /dev/null +++ b/sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj @@ -0,0 +1,109 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + 18.0 + {5D0218F2-E502-414F-BA66-9E76B2785178} + sample_prime_numbers + Win32Proj + + + + Application + v145 + Unicode + true + + + Application + v145 + Unicode + + + + + + + + + + + + + <_ProjectFileVersion>18.0.11123.170 + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + true + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + false + + + + Disabled + ../../../include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + Level3 + EditAndContinue + + + bitfield.lib;%(AdditionalDependencies) + ../Debug;%(AdditionalLibraryDirectories) + true + Console + MachineX86 + + + + + MaxSpeed + true + ../../../include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + Level3 + ProgramDatabase + + + bitfield.lib;%(AdditionalDependencies) + ../Release;%(AdditionalLibraryDirectories) + true + Console + true + true + MachineX86 + + + + + {a76282b3-4276-4945-9af2-3ac68111a828} + true + true + + + + + + + + + \ No newline at end of file diff --git a/sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj.filters b/sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj.filters new file mode 100644 index 000000000..11ea010d9 --- /dev/null +++ b/sln/vc9/sample_prime_numbers/sample_prime_numbers.vcxproj.filters @@ -0,0 +1,14 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + \ No newline at end of file diff --git a/sln/vc9/test_bitfield/test_bitfield.vcxproj b/sln/vc9/test_bitfield/test_bitfield.vcxproj new file mode 100644 index 000000000..3defc674b --- /dev/null +++ b/sln/vc9/test_bitfield/test_bitfield.vcxproj @@ -0,0 +1,116 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + 18.0 + {C650C93E-F0A7-4235-9F5F-0DCE78609BFB} + test_bitfield + Win32Proj + + + + Application + v145 + Unicode + true + + + Application + v145 + Unicode + + + + + + + + + + + + + <_ProjectFileVersion>18.0.11123.170 + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + true + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + false + + + + Disabled + ../../../gtest;../../../include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + Level3 + EditAndContinue + + + gtest.lib;bitfield.lib;%(AdditionalDependencies) + ../Debug;%(AdditionalLibraryDirectories) + true + Console + MachineX86 + + + + + MaxSpeed + true + ../../../gtest;../../../include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + Level3 + ProgramDatabase + + + gtest.lib;bitfield.lib;%(AdditionalDependencies) + ../Release;%(AdditionalLibraryDirectories) + true + Console + true + true + MachineX86 + + + + + {a76282b3-4276-4945-9af2-3ac68111a828} + true + true + + + {60932afc-7808-48b7-b3bf-f2bc151c0065} + true + true + + + + + + + + + + + \ No newline at end of file diff --git a/sln/vc9/test_bitfield/test_bitfield.vcxproj.filters b/sln/vc9/test_bitfield/test_bitfield.vcxproj.filters new file mode 100644 index 000000000..ef84815fb --- /dev/null +++ b/sln/vc9/test_bitfield/test_bitfield.vcxproj.filters @@ -0,0 +1,20 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/src/tbitfield.cpp b/src/tbitfield.cpp index 055b91eee..22515122d 100644 --- a/src/tbitfield.cpp +++ b/src/tbitfield.cpp @@ -7,88 +7,213 @@ #include "tbitfield.h" +// Fake variables used as placeholders in tests +static const int FAKE_INT = -1; +static TBitField FAKE_BITFIELD(1); +size_t TBitField::GET_INDEX_CONST = mylog2(8 * sizeof(TELEM)); + + TBitField::TBitField(int len) { + if (len <= 0) + throw out_of_range("Len must be > 0."); + BitLen = len; + MemLen = GetMemIndex(BitLen) + 1; + pMem = new TELEM[MemLen]; + for (size_t i = 0; i < MemLen; i++) + { + pMem[i] = 0; + } } -TBitField::TBitField(const TBitField &bf) // конструктор копирования +TBitField::TBitField(const TBitField& bf) // конструктор копирования { + MemLen = bf.MemLen; + BitLen = bf.BitLen; + pMem = new TELEM[MemLen]; + for (size_t i = 0; i < MemLen; i++) + pMem[i] = bf.pMem[i]; } TBitField::~TBitField() { + delete[] pMem; } int TBitField::GetMemIndex(const int n) const // индекс Мем для бита n { - return 0; + return n >> TBitField::GET_INDEX_CONST; } TELEM TBitField::GetMemMask(const int n) const // битовая маска для бита n { - return TELEM(); + TELEM m = 1; + return m << (n & (sizeof(TELEM) * 8 - 1)); } // доступ к битам битового поля int TBitField::GetLength(void) const // получить длину (к-во битов) { - return 0; + return BitLen; } void TBitField::SetBit(const int n) // установить бит { + if (n < 0) + throw out_of_range("n must be > 0"); + if (n >= BitLen) + throw out_of_range("n must be less than BitLien"); + pMem[GetMemIndex(n)] |= GetMemMask(n); } void TBitField::ClrBit(const int n) // очистить бит { + if (n < 0) + throw out_of_range("n must be > 0"); + if (n >= BitLen) + throw out_of_range("n must be less than BitLien"); + pMem[GetMemIndex(n)] &= ~GetMemMask(n); } int TBitField::GetBit(const int n) const // получить значение бита { - return 0; + if (n < 0) + throw out_of_range("n must be > 0"); + if (n >= BitLen) + throw out_of_range("n must be less than BitLien"); + return (pMem[GetMemIndex(n)] & GetMemMask(n)) != 0; } // битовые операции -TBitField& TBitField::operator=(const TBitField &bf) // присваивание -{ +TBitField& TBitField::operator=(const TBitField& bf) // присваивание +{ + if (pMem == bf.pMem) + return *this; + BitLen = bf.BitLen; + if (MemLen != bf.MemLen || pMem == nullptr) + { + delete[] pMem; + MemLen = bf.MemLen; + pMem = new TELEM[MemLen]; + } + for (size_t i = 0; i < MemLen; i++) + pMem[i] = bf.pMem[i]; return *this; } -int TBitField::operator==(const TBitField &bf) const // сравнение -{ - return 0; -} - -int TBitField::operator!=(const TBitField &bf) const // сравнение -{ - return 0; -} - -TBitField TBitField::operator|(const TBitField &bf) // операция "или" -{ - return TBitField(0); -} - -TBitField TBitField::operator&(const TBitField &bf) // операция "и" -{ - return TBitField(0); +int TBitField::operator==(const TBitField& bf) const // сравнение +{ + if (BitLen != bf.BitLen) + return false; + for (int i = 0; i < BitLen; i++) + if (GetBit(i) != bf.GetBit(i)) + return false; + return true; +} + +int TBitField::operator!=(const TBitField& bf) const // сравнение +{ + if (BitLen != bf.BitLen) + return true; + for (int i = 0; i < BitLen; i++) + if (GetBit(i) != bf.GetBit(i)) + return true; + return false; +} + +TBitField TBitField::operator|(const TBitField& bf) // операция "или" +{ + int highBitLen; + int lowBitLen; + const TBitField* highBitField; + if (BitLen > bf.BitLen) + { + highBitLen = BitLen; + lowBitLen = bf.BitLen; + highBitField = this; + } + else + { + highBitLen = bf.BitLen; + lowBitLen = BitLen; + highBitField = &bf; + } + TBitField tmp(highBitLen); + int i; + for (i = 0; i < lowBitLen; i++) + if (GetBit(i) | bf.GetBit(i)) + tmp.SetBit(i); + for (; i < highBitLen; i++) + if (highBitField->GetBit(i)) + tmp.SetBit(i); + return tmp; +} + +TBitField TBitField::operator&(const TBitField& bf) // операция "и" +{ + int highBitLen; + int lowBitLen; + if (BitLen > bf.BitLen) + { + highBitLen = BitLen; + lowBitLen = bf.BitLen; + } + else + { + highBitLen = bf.BitLen; + lowBitLen = BitLen; + } + TBitField tmp(highBitLen); + for (int i = 0; i < lowBitLen; i++) + if (GetBit(i) & bf.GetBit(i)) + tmp.SetBit(i); + return tmp; } TBitField TBitField::operator~(void) // отрицание { - return TBitField(0); + TBitField tmp(BitLen); + int i; + for (i = 0; i < MemLen - 1; i++) + tmp.pMem[i] = ~pMem[i]; + for (i = (MemLen - 1) * 8 * sizeof(TELEM); i < BitLen; i++) + if (!GetBit(i)) + tmp.SetBit(i); + return tmp; } // ввод/вывод -istream &operator>>(istream &istr, TBitField &bf) // ввод -{ +istream& operator>>(istream& istr, TBitField& bf) // ввод +{ + const int IN_BUFFER = 5; + char c; + int i = 0; + bf = TBitField(1); + + while (istr.get(c) && c != 10) + { + if (i % IN_BUFFER == 0) + bf = bf | TBitField(i + IN_BUFFER); + if (c != '0') + bf.SetBit(i); + i++; + } + if (bf.BitLen > i) + { + TBitField tmp = bf; + bf = TBitField(i); + for (i = 0; i < bf.MemLen; i++) + bf.pMem[i] = tmp.pMem[i]; + } return istr; } -ostream &operator<<(ostream &ostr, const TBitField &bf) // вывод +ostream& operator<<(ostream& ostr, const TBitField& bf) // вывод { + for (int i = 0; i < bf.BitLen; i++) + ostr << bf.GetBit(i); return ostr; } diff --git a/src/tset.cpp b/src/tset.cpp index 73cb72635..e35732f6a 100644 --- a/src/tset.cpp +++ b/src/tset.cpp @@ -7,93 +7,157 @@ #include "tset.h" -TSet::TSet(int mp) : BitField(-1) +// Fake variables used as placeholders in tests +static const int FAKE_INT = -1; +static TBitField FAKE_BITFIELD(1); +static TSet FAKE_SET(1); + +TSet::TSet(int mp) : MaxPower(mp), BitField(mp) { } // конструктор копирования -TSet::TSet(const TSet &s) : BitField(-1) +TSet::TSet(const TSet& s) : MaxPower(s.MaxPower), BitField(s.BitField) { } // конструктор преобразования типа -TSet::TSet(const TBitField &bf) : BitField(-1) +TSet::TSet(const TBitField& bf) : MaxPower(bf.GetLength()), BitField(bf) { } TSet::operator TBitField() { - return TBitField(0); + return TBitField(BitField); } int TSet::GetMaxPower(void) const // получить макс. к-во эл-тов { - return 0; + return MaxPower; } int TSet::IsMember(const int Elem) const // элемент множества? { - return 0; + if (Elem >= MaxPower) + return false; + return BitField.GetBit(Elem); } void TSet::InsElem(const int Elem) // включение элемента множества { + if (Elem >= MaxPower) + throw out_of_range("Elem is out of universe"); + BitField.SetBit(Elem); } void TSet::DelElem(const int Elem) // исключение элемента множества { + if (Elem >= MaxPower) + throw out_of_range("Elem is out of universe"); + BitField.ClrBit(Elem); } // теоретико-множественные операции -TSet& TSet::operator=(const TSet &s) // присваивание +TSet& TSet::operator=(const TSet& s) // присваивание { + BitField = s.BitField; + MaxPower = s.MaxPower; return *this; } -int TSet::operator==(const TSet &s) const // сравнение +int TSet::operator==(const TSet& s) const // сравнение { - return 0; + return BitField == s.BitField; } -int TSet::operator!=(const TSet &s) const // сравнение +int TSet::operator!=(const TSet& s) const // сравнение { - return 0; + return BitField != s.BitField; } -TSet TSet::operator+(const TSet &s) // объединение +TSet TSet::operator+(const TSet& s) // объединение { - return TSet(0); + return TSet(BitField | s.BitField); } TSet TSet::operator+(const int Elem) // объединение с элементом { - return TSet(0); + TSet tmp(BitField); + tmp.InsElem(Elem); + return tmp; } TSet TSet::operator-(const int Elem) // разность с элементом { - return TSet(0); + TSet tmp(BitField); + tmp.DelElem(Elem); + return tmp; } -TSet TSet::operator*(const TSet &s) // пересечение +TSet TSet::operator*(const TSet& s) // пересечение { - return TSet(0); + return TSet(BitField & s.BitField); } TSet TSet::operator~(void) // дополнение { - return TSet(0); + return TSet(~BitField); } // перегрузка ввода/вывода -istream &operator>>(istream &istr, TSet &s) // ввод -{ +istream& operator>>(istream& istr, TSet& s) // ввод +{ + const int IN_BUFFER = 100; + int temp_num[IN_BUFFER]; + char c = '0'; + int k = 1, num = 0, size = 0; + s = TSet(1); + + while (c && c != 10) + { + istr.get(c); + if (c == ',' || !c || c == 10) + { + if (size == 0) + throw logic_error("Bad input, can't have null character in set"); + for (int i = size - 1; i >= 0; i--) + { + num += temp_num[i] * k; + k *= 10; + } + if (s.MaxPower <= num) + s = s + TSet(num + 1); + s.InsElem(num); + k = 1; + num = 0; + size = 0; + } + else + { + if (c < '0' || c > '9') + throw logic_error("Bad input, invalid character, must be a number"); + if (size >= IN_BUFFER) + throw overflow_error("Number is too long"); + temp_num[size++] = c - '0'; + } + } return istr; } -ostream& operator<<(ostream &ostr, const TSet &s) // вывод +ostream& operator<<(ostream& ostr, const TSet& s) // вывод { + ostr << '{'; + bool found_first = false; + for (int i = 0; i < s.MaxPower; i++) + if (s.IsMember(i)) + { + if (found_first) + ostr << ','; + found_first = true; + ostr << ' ' << i; + } + ostr << ' ' << '}'; return ostr; } diff --git a/test/test_main.cpp b/test/test_main.cpp index 3968c279a..f7a986ba8 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -1,6 +1,7 @@ -#include +#include +#include "gtest.h" int main(int argc, char **argv) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/test/test_tbitfield.cpp b/test/test_tbitfield.cpp index fa388ab1c..7fff5ee49 100644 --- a/test/test_tbitfield.cpp +++ b/test/test_tbitfield.cpp @@ -2,7 +2,8 @@ #include -TEST(TBitField, can_create_bitfield_with_positive_length) +TEST(TBitField, can_create_bitfield_with_positive_length)//1123 + { ASSERT_NO_THROW(TBitField bf(3)); } diff --git a/test/test_tset.cpp b/test/test_tset.cpp index 10405d7d7..0a9f2139c 100644 --- a/test/test_tset.cpp +++ b/test/test_tset.cpp @@ -2,7 +2,7 @@ #include -TEST(TSet, can_get_max_power_set) +TEST(TSet, can_get_max_power_set)//1 { const int size = 5; TSet set(size);