diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8bd026 --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..75c212a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Catch"] + path = Catch + url = https://github.com/philsquared/Catch diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..aa21156 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +OhString \ No newline at end of file diff --git a/.idea/OhString.iml b/.idea/OhString.iml new file mode 100644 index 0000000..f19c8da --- /dev/null +++ b/.idea/OhString.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3eb495b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1abeea8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..64c004e --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,650 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1457949972084 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d65ed7f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,31 @@ +sudo: false + +language: cpp + +compiler: + - clang + - gcc + +before_install: + - pip install --user cpp-coveralls + +install: + - if [ "$CXX" = "g++" ]; then export CXX="g++-4.9" CC="gcc-4.9"; fi + - if [ "$CXX" = "clang++" ]; then export CXX="clang++-3.7" CC="clang-3.7"; fi + - mkdir build + - cd build + - cmake .. + - make + +script: + - ctest -V + +addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.7 + packages: + - gcc-4.9 + - g++-4.9 + - clang-3.7 \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7feb669 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 2.8) +project(CppLab2) + +if (MSVC) + add_definitions(/W4) +else() + add_definitions(-Wall) + add_definitions(-std=c++11) +endif() + +include_directories(Catch/include) +include_directories(include) + +enable_testing() + +set(SOURCES src/string.cpp) + +add_executable(test_string tests/test_string.cpp ${SOURCES}) + +add_test(NAME ${PROJECT_NAME} COMMAND test_string) \ No newline at end of file diff --git a/Catch b/Catch new file mode 160000 index 0000000..6de7142 --- /dev/null +++ b/Catch @@ -0,0 +1 @@ +Subproject commit 6de7142d1fcf3075bd17d3f6fa2ee76ce4c85cf8 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..5bf4c60 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2016 sdukshis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md index e69de29..4aef36f 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,3 @@ +# *KENTAVOR * +[![Build status](https://ci.appveyor.com/api/projects/status/6opjf5ooniww1pf3/branch/dev?svg=true)](https://ci.appveyor.com/project/Engage222/laba2-3qflw/branch/dev) +[![Build Status](https://travis-ci.org/Engage222/laba2.svg?branch=dev)](https://travis-ci.org/Engage222/laba2) \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..7ddbd3a --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,20 @@ +version: 1.0.{build} + +clone_folder: c:\dev\complex + +build: + +install: + - git submodule -q update --init --recursive + +build_script: + - cd c:\dev\complex + - md build + - cd build + - cmake .. + - cmake --build . --config debug + +after_build: + +test_script: + - cmd: ctest -C Debug -VV diff --git a/include/MyString.h b/include/MyString.h new file mode 100644 index 0000000..01acd85 --- /dev/null +++ b/include/MyString.h @@ -0,0 +1,45 @@ +#ifndef MYSTRING_H +#define MYSTRING_H +#include + +class String { +private: + unsigned int size_st; + char *str_; + static char pos_; +public: + String(); + String(const char *str); + String(const char *str, unsigned count); + String(char ch, unsigned count); + String(const String &other); + String(String &&other); + ~String(); + String & operator=(const String &other); + String & operator=(String &&other); + + String & operator+=(const String &suffix); + String & operator+=(const char *suffix); + String & operator+=(char suffix); + void swap(String &other); + char & operator[](unsigned pos); + const char operator[](unsigned pos) const; + + char & at(unsigned int pos); + const char at (unsigned int pos)const ; + + const char * data() const; + + unsigned int size() const; + + friend bool operator==(const String &lhs, const String &rhs); + friend bool operator<(const String &lhs, const String &rhs); +}; +String operator+(const String &lhs, const String &rhs); +String operator+(const String &lhs, const char *rhs); +String operator+(const char *lhs, const String &rhs); +bool operator!=(const String &lhs, const String &rhs); +bool operator<=(const String &lhs, const String &rhs); +bool operator>(const String &lhs, const String &rhs); +bool operator>=(const String &lhs, const String &rhs); +#endif diff --git a/src/string.cpp b/src/string.cpp new file mode 100644 index 0000000..8a17236 --- /dev/null +++ b/src/string.cpp @@ -0,0 +1,176 @@ +#include +#include +#include "MyString.h" + +char String::pos_ = '\0'; + +String::String() : str_(&pos_), size_st(0) {} + +String::String(const char *str) : size_st(std::strlen(str)) { + str_=new char[size_st+1]; + std::strcpy(str_, str); +} + +String::String(const char *str, unsigned count) : size_st(count) { + str_ = new char [count + 1]; + for (unsigned int i = 0; i < count; i++){ + str_[i] = str[i]; + } + str_[count] = pos_; +} + +String::String(char ch, unsigned count) : size_st(count) { + str_ = new char[count + 1]; + for (unsigned i = 0; i < count; ++i ){ + str_[i] = ch; + } + str_[count] = '\0'; + size_st= count; +} + +String::String(const String &other) : size_st(other.size_st) { + str_ = new char[size_st+1]; + std::strcpy(str_, other.str_); +} + +String::String(String &&other) : str_(other.str_), size_st(other.size_st) { + other.str_ = &pos_; + other.size_st = 0; +} + +String::~String() { + if (str_ != &pos_) { + delete[] str_; + } +} + +String &String::operator=(const String &other){ + String b(other); + this->swap(b); + return *this; +} + +String &String::operator=(String &&other){ + if (&(other) != this) { + if (str_ != &pos_) { + delete[] str_; + } + str_ = other.str_; + size_st = other.size_st; + other.str_ = &pos_; + other.size_st = 0; + } + return *this; +} + +String &String::operator+=(const String &suffix) { + char *a = new char[size_st + suffix.size_st + 1]; + std::strcpy(a, str_); + std::strcat(a,suffix.str_); + if (str_ != &pos_) { + delete[] str_; + } + str_ = a; + size_st += suffix.size_st; + return *this; +} + +String &String::operator+=(const char *suffix) { + char *a = new char[size_st + std::strlen(suffix) + 1]; + std::strcpy(a, str_); + std::strcat(a,suffix); + if (str_ != &pos_) { + delete[] str_; + } + str_ = a; + size_st += std::strlen(suffix); + return *this; +} + +String &String::operator+=(char suffix) { + String b(suffix,1); + char *a = new char[size_st + 2]; + std::strcpy(a,str_); + std::strcat(a,b.str_); + if (str_ != &pos_) { + delete[] str_; + } + str_ = a; + size_st += 1; + return *this; +} + +void String::swap(String &other){ + std::swap (str_,other.str_); + std::swap (size_st,other.size_st); +} + +char &String::operator[](unsigned pos){ + return str_[pos]; +} + +const char String::operator[](unsigned pos) const{ + return str_[pos]; +} + +char &String::at(unsigned int pos) { + if (pos >= size_st) { + throw std::out_of_range("positon >= size of string"); + } + return str_[pos]; +} + +const char String::at(unsigned int pos) const { + if (pos >= size_st) { + throw std::out_of_range("positon >= size of string"); + } + return str_[pos]; +} + +const char *String:: data() const{ + return str_; +} + +unsigned int String::size() const{ + return size_st; +} + +bool operator==(const String &lhs, const String &rhs){ + return !std::strcmp(lhs.str_, rhs.str_); +} + +bool operator<(const String &lhs, const String &rhs) { + return std::strcmp(lhs.str_, rhs.str_) < 0; +} + +String operator+(const String &lhs, const String &rhs) { + return String(lhs) += rhs; +} + +String operator+(const String &lhs, const char *rhs) { + return String(lhs) += rhs; +} + +String operator+(const char *lhs, const String &rhs) { + return String(rhs) += lhs; +} + +bool operator!=(const String &lhs, const String &rhs) { + return !(lhs == rhs); +} + +bool operator<=(const String &lhs, const String &rhs) { + return (lhs == rhs) || (lhs < rhs); +} + +bool operator>(const String &lhs, const String &rhs) { + return !(lhs <= rhs); +} + +bool operator>=(const String &lhs, const String &rhs) { + return !(lhs < rhs); +} + +std::ostream &operator<<(std::ostream &stream, const String &A) { + return stream << A.data(); +} diff --git a/tests/test_string.cpp b/tests/test_string.cpp new file mode 100644 index 0000000..04aa3ea --- /dev/null +++ b/tests/test_string.cpp @@ -0,0 +1,254 @@ +#include "MyString.h" +#include +#include +#define CATCH_CONFIG_MAIN +#include "catch.hpp" + + + + +TEST_CASE("TEST 1") { + String s; + REQUIRE(*(s.data()) == '\0'); + REQUIRE(s.size() == 0); +} + +TEST_CASE("TEST 2") { + char B[] = "Test2"; + String A(B); + const char *pt = A.data(); + REQUIRE(A.size() == 5); + for (unsigned i = 0; i <= A.size(); ++i) { + REQUIRE(B[i] == *(pt++)); + } +} + +TEST_CASE("TEST 3") { + char B[] = "Test3"; + String A(B, 5); + const char *pt = A.data(); + REQUIRE(A.size() == 5); + for (unsigned i = 0; i <= A.size(); ++i) { + REQUIRE(B[i] == *(pt++)); + } +} + +TEST_CASE("Test 4") { + char B = 'q'; + String A(B, 1); + const char *pt = A.data(); + REQUIRE(A.size() == 1); + for (unsigned i = 0; i < A.size(); ++i) { + REQUIRE(B == *(pt++)); + } + REQUIRE('\0' == *pt); +} + +TEST_CASE("Test5") { + String B("Test5"); + String A(B); + REQUIRE(A.size() == B.size()); + for (unsigned i = 0; i <= A.size(); ++i) { + REQUIRE(B[i] == A[i]); + } +} +TEST_CASE("Test6") { + String B("Test6"); + String C(B); + String A(std::move(B)); + REQUIRE(A.size() == C.size()); + for (unsigned i = 0; i <= A.size(); ++i) { + REQUIRE(C[i] == A[i]); + } + REQUIRE(B.size() == 0); + REQUIRE(*(B.data()) == '\0'); +} + +TEST_CASE ("Test_assigment1") { + String A("Test_assigment1"); + String B, C; + C = B = A; + REQUIRE(A.size() == C.size()); + for (unsigned i = 0; i <= A.size(); ++i) { + REQUIRE(C[i] == A[i]); + } +} + +TEST_CASE ("Test_assigment2") { + String B("Test_assigment2"); + String C = B; + String A = std::move(B); + REQUIRE(A.size() == C.size()); + for (unsigned i = 0; i <= A.size(); ++i) { + REQUIRE(C[i] == A[i]); + } + REQUIRE(B.size() == 0); + REQUIRE(*(B.data()) == '\0'); +} + +TEST_CASE ("Test_plus_assigment_1") { + char res[] = "plus_assigment"; + String A("plus_"); + size_t Asize = A.size(); + String B("assigment"); + size_t Bsize = B.size(); + A += B; + std::cout< B); + +} + +TEST_CASE ("Test_more_equality") { + String A("more"); + String B("more"); + String C("equality"); + REQUIRE(((B >= A) && (A >= C))); + +} \ No newline at end of file