Changeset ed81b1f in mainline


Ignore:
Timestamp:
2018-07-05T21:41:18Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e502572b
Parents:
2d302d6
git-author:
Jaroslav Jindrak <dzejrou@…> (2017-11-01 19:07:07)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:18)
Message:

cpp: finished std::string::insert tests and fixed some problems with length and other bugs revealed by the tests

Location:
uspace/lib/cpp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/string.hpp

    r2d302d6 red81b1f  
    331331                : data_{}, size_{}, capacity_{}, allocator_{alloc}
    332332            {
    333                 init_(str, traits_type::length(str) + 1);
     333                init_(str, traits_type::length(str));
    334334            }
    335335
     
    361361                {
    362362                    auto len = static_cast<size_type>(last - first);
    363                     init_(static_cast<value_type*>(first), len);
     363                    init_(first, len);
    364364                }
    365365            }
     
    501501            size_type length() const noexcept
    502502            {
    503                 return size_;
     503                return size();
    504504            }
    505505
     
    652652                // TODO: if (size_ + n > max_size()) throw length_error
    653653                ensure_free_space_(n);
    654                 traits_type::copy(data_ + size_ - 1, str, n);
    655                 size_ += n - 1; // We are not copying str's null terminator.
     654                traits_type::copy(data_ + size(), str, n);
     655                size_ += n;
    656656                ensure_null_terminator_();
    657657
     
    661661            basic_string& append(const value_type* str)
    662662            {
    663                 return append(str, traits_type::length(str) + 1);
     663                return append(str, traits_type::length(str));
    664664            }
    665665
     
    769769
    770770                copy_backward_(begin() + pos, end(), end() + n);
    771                 std::printf("|%s|\n", data_);
    772771                copy_(str, str + n, begin() + pos);
    773772                size_ += n;
  • uspace/lib/cpp/src/internal/test/string.cpp

    r2d302d6 red81b1f  
    5555        test_eq(
    5656            "size of string",
    57             str1.size(), 6ul
     57            str1.size(), 5ul
    5858        );
    5959        test_eq(
    6060            "initialization from a cstring literal",
    6161            str1.begin(), str1.end(),
    62             check1, check1 + 6
     62            check1, check1 + 5
    6363        );
    6464
     
    140140
    141141        std::string str5{"hello, "};
    142         str5.append({'w', 'o', 'r', 'l', 'd', '\0'});
     142        str5.append({'w', 'o', 'r', 'l', 'd'});
    143143        test_eq(
    144144            "append initializer list",
     
    187187        std::string insertee{"ello"};
    188188        str4.insert(str4.begin() + 1, insertee.begin(),
    189                     insertee.end() - 1);
     189                    insertee.end());
    190190        test_eq(
    191191            "insert iterator range",
     
    193193            check.begin(), check.end()
    194194        );
    195         /* std::printf("|%s|\n", str4.c_str()); */
     195
     196        std::string str5{"hel, world"};
     197        std::initializer_list<char> init{'l', 'o'};
     198        str5.insert(str5.begin() + 3, init);
     199        test_eq(
     200            "insert initializer list",
     201            str5.begin(), str5.end(),
     202            check.begin(), check.end()
     203        );
    196204    }
    197205}
Note: See TracChangeset for help on using the changeset viewer.