aboutsummaryrefslogtreecommitdiff
path: root/tests/small-vector/driver.cxx
blob: 38948f8087fe57b8e257aaca310c270f9bcfc96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// file      : tests/small-vector/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules
#include <string>
#include <iostream>
#endif

// Other includes.

#ifdef __cpp_modules
#ifdef __cpp_lib_modules
import std.core;
import std.io;
#endif
import butl.small_vector;
#else
#include <libbutl/small-vector.mxx>
#endif

using namespace std;
using namespace butl;

// Return true if v.data() points to somewhere inside v.
//
template <typename T, size_t N>
inline bool
small (const small_vector<T, N>& v)
{
  const void* d (v.data ());
  return d >= &v && d < (&v + 1);
}

int
main ()
{
  using vector = small_vector<string, 2>;

  {
    vector v;
    assert (v.capacity () == 2 && small (v));

    v.push_back ("abc");
    assert (v[0] == "abc" && v.capacity () == 2 && small (v));

    v.push_back ("ABC");
    assert (v[1] == "ABC" && v.capacity () == 2 && small (v));

    string* d (v.data ());              // Small buffer...

    v.push_back ("xyz");
    assert (v[0] == "abc" && v.data () != d && !small (v));

    v.pop_back ();
    v.shrink_to_fit ();
    assert (v[0] == "abc" && v.data () == d);
  }

  // Allocator comparison.
  //
  {
    vector v1, v2;
    assert (v1.get_allocator () != v2.get_allocator ()); // stack/stack

    v1.assign ({"abc", "ABC", "xyz"});
    assert (v1.get_allocator () != v2.get_allocator ()); // heap/stack

    v2.assign ({"abc", "ABC", "xyz"});
    assert (v1.get_allocator () == v2.get_allocator ()); // heap/heap

    v1.pop_back ();
    v1.shrink_to_fit ();
    assert (v1.get_allocator () != v2.get_allocator ()); // stack/heap

    v2.pop_back ();
    v2.shrink_to_fit ();
    assert (v1.get_allocator () != v2.get_allocator ()); // stack/stack
  }

  // Copy constructor.
  //
  {
    vector s1 ({"abc"}), s2 (s1);
    assert (s1 == s2 && s2.capacity () == 2 && small (s2));

    vector l1 ({"abc", "ABC", "xyz"}), l2 (l1);
    assert (l1 == l2 && !small (l2));
  }

  // Move constructor.
  //
  {
    struct mstring: string // Move-only string.
    {
      mstring () = default;
      explicit mstring (const char* s): string (s) {}

      mstring (mstring&&) = default;
      mstring& operator= (mstring&&) = default;

      mstring (const mstring&) = delete;
      mstring& operator= (const mstring&) = delete;
    };

    using vector = small_vector<mstring, 2>;

    {
      vector s1;
      s1.emplace_back ("abc");
      vector s2 (move (s1));
      assert (s2[0] == "abc" && s2.capacity () == 2 && small (s2));
    }

    {
      vector s1;
      s1.emplace_back ("abc");
      s1.emplace_back ("ABC");
      vector s2 (move (s1));
      assert (s2[0] == "abc" && s2[1] == "ABC" &&
              s2.capacity () == 2 && small (s2));
    }

    vector l1;
    l1.emplace_back ("abc");
    l1.emplace_back ("ABC");
    l1.emplace_back ("xyz");
    vector l2 (move (l1));
    assert (l2[0] == "abc" && l2[1] == "ABC" && l2[2] == "xyz" && !small (l2));
  }

  // Other constructors.
  //

  {
    const char* sa[] = {"abc"};
    const char* la[] = {"abc", "ABC", "xyz"};

    vector s (sa, sa + 1);
    assert (s[0] == "abc" && s.capacity () == 2 && small (s));

    vector l (la, la + 3);
    assert (l[0] == "abc" && l[1] == "ABC" && l[2] == "xyz" && !small (l));
  }

  {
    vector s (1, "abc");
    assert (s[0] == "abc" && s.capacity () == 2 && small (s));

    vector l (3, "abc");
    assert (l[0] == "abc" && l[2] == "abc" && !small (l));
  }

  {
    vector s (1);
    assert (s[0] == "" && s.capacity () == 2 && small (s));

    vector l (3);
    assert (l[0] == "" && l[2] == "" && !small (l));
  }
}