Skip to content
This repository was archived by the owner on Jun 15, 2025. It is now read-only.

Commit 926862c

Browse files
committed
fix #19: fixed insert() iterator types + added tests
1 parent b2cd7e5 commit 926862c

File tree

2 files changed

+128
-4
lines changed

2 files changed

+128
-4
lines changed

include/spsl/stringbase.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,16 @@ class StringBase : public StringCore<StorageType>
218218
iterator insert(const_iterator pos, char_type ch) { return insert(pos, 1, ch); }
219219
iterator insert(const_iterator pos, size_type count, char_type ch)
220220
{
221-
m_storage.insert(pos - begin(), count, ch);
222-
return pos;
221+
const auto offset = static_cast<size_t>(pos - begin());
222+
m_storage.insert(offset, count, ch);
223+
return begin() + offset;
223224
}
224225
template <class InputIt, typename = checkInputIter<InputIt>>
225226
iterator insert(const_iterator pos, InputIt first, InputIt last)
226227
{
227-
m_storage.insert(pos - begin(), first, last);
228-
return pos;
228+
const auto offset = static_cast<size_t>(pos - begin());
229+
m_storage.insert(offset, first, last);
230+
return begin() + offset;
229231
}
230232
iterator insert(const_iterator pos, std::initializer_list<char_type> ilist)
231233
{

test/test_stringbase.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,128 @@ TYPED_TEST(StringBaseTest, ReplaceFunctions)
13671367
}
13681368
}
13691369

1370+
/* insert functions */
1371+
TYPED_TEST(StringBaseTest, InsertFunctions)
1372+
{
1373+
using StringType = TypeParam; // gtest specific
1374+
using StorageType = typename StringType::storage_type;
1375+
using CharType = typename StorageType::char_type;
1376+
using Traits = typename StorageType::traits_type;
1377+
const TestData<CharType> data{};
1378+
1379+
using RefType = std::basic_string<CharType>;
1380+
1381+
// this_type& insert(size_type index, size_type count, char_type ch)
1382+
{
1383+
StringType s1(data.hello_world);
1384+
RefType s2(data.hello_world);
1385+
1386+
s1.insert(3, 13, data.hello_world[0]);
1387+
s2.insert(3, 13, data.hello_world[0]);
1388+
ASSERT_EQ(s1.size(), s2.size());
1389+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1390+
}
1391+
// this_type& insert(size_type index, const char_type* s)
1392+
{
1393+
StringType s1(data.hello_world);
1394+
RefType s2(data.hello_world);
1395+
1396+
s1.insert(0, data.blablabla);
1397+
s2.insert(0, data.blablabla);
1398+
ASSERT_EQ(s1.size(), s2.size());
1399+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1400+
}
1401+
// this_type& insert(size_type index, const char_type* s, size_type count)
1402+
{
1403+
StringType s1(data.hello_world);
1404+
RefType s2(data.hello_world);
1405+
1406+
s1.insert(s1.size() - 2, data.blablabla, data.blablabla_len - 1);
1407+
s2.insert(s2.size() - 2, data.blablabla, data.blablabla_len - 1);
1408+
ASSERT_EQ(s1.size(), s2.size());
1409+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1410+
}
1411+
// this_type& insert(size_type index, const StringClass& s)
1412+
{
1413+
StringType s1(data.hello_world);
1414+
RefType s2(data.hello_world);
1415+
// we use basic_string here
1416+
const RefType ins(data.blablabla);
1417+
1418+
s1.insert(1, ins);
1419+
s2.insert(1, ins);
1420+
ASSERT_EQ(s1.size(), s2.size());
1421+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1422+
}
1423+
// this_type& insert(size_type, const StringClass&, size_type, size_type count)
1424+
{
1425+
StringType s1(data.hello_world);
1426+
RefType s2(data.hello_world);
1427+
// we use basic_string here
1428+
const RefType ins(data.blablabla);
1429+
1430+
s1.insert(1, ins, 3, StringType::npos);
1431+
s2.insert(1, ins, 3, RefType::npos);
1432+
ASSERT_EQ(s1.size(), s2.size());
1433+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1434+
}
1435+
1436+
// iterator insert(const_iterator pos, char_type ch)
1437+
{
1438+
StringType s1(data.hello_world);
1439+
RefType s2(data.hello_world);
1440+
1441+
s1.insert(s1.begin() + 5, data.hello_world[0]);
1442+
s2.insert(s2.begin() + 5, data.hello_world[0]);
1443+
ASSERT_EQ(s1.size(), s2.size());
1444+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1445+
}
1446+
// iterator insert(const_iterator pos, size_type count, char_type ch)
1447+
{
1448+
StringType s1(data.hello_world);
1449+
RefType s2(data.hello_world);
1450+
1451+
s1.insert(s1.begin(), 9, data.hello_world[0]);
1452+
s2.insert(s2.begin(), 9, data.hello_world[0]);
1453+
ASSERT_EQ(s1.size(), s2.size());
1454+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1455+
}
1456+
// iterator insert(const_iterator pos, InputIt first, InputIt last)
1457+
{
1458+
StringType s1(data.hello_world);
1459+
RefType s2(data.hello_world);
1460+
RefType ins(data.blablabla);
1461+
1462+
// "normal" iterator
1463+
s1.insert(s1.begin() + 2, ins.begin(), ins.end());
1464+
s2.insert(s2.begin() + 2, ins.begin(), ins.end());
1465+
ASSERT_EQ(s1.size(), s2.size());
1466+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1467+
1468+
// const_iterator
1469+
s1.insert(s1.begin() + 2, ins.cbegin(), ins.cbegin() + 6);
1470+
s2.insert(s2.begin() + 2, ins.cbegin(), ins.cbegin() + 6);
1471+
ASSERT_EQ(s1.size(), s2.size());
1472+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1473+
1474+
// reverse_iterator
1475+
s1.insert(s1.begin() + 2, ins.rbegin(), ins.rend());
1476+
s2.insert(s2.begin() + 2, ins.rbegin(), ins.rend());
1477+
ASSERT_EQ(s1.size(), s2.size());
1478+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1479+
}
1480+
// iterator insert(const_iterator pos, std::initializer_list<char_type> ilist)
1481+
{
1482+
StringType s1(data.hello_world);
1483+
RefType s2(data.hello_world);
1484+
1485+
s1.insert(s1.begin(), data.initializerList2());
1486+
s2.insert(s2.begin(), data.initializerList2());
1487+
ASSERT_EQ(s1.size(), s2.size());
1488+
ASSERT_TRUE(Traits::compare(s1.data(), s2.data(), s1.size()) == 0);
1489+
}
1490+
}
1491+
13701492
/* operator<< */
13711493
TYPED_TEST(StringBaseTest, OutputStream)
13721494
{

0 commit comments

Comments
 (0)