File: copy_assign.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (170 lines) | stat: -rw-r--r-- 5,445 bytes parent folder | download | duplicates (7)
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
164
165
166
167
168
169
170
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: can-create-symlinks
// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: no-filesystem
// UNSUPPORTED: availability-filesystem-missing

// <filesystem>

// class recursive_directory_iterator

// recursive_directory_iterator& operator=(recursive_directory_iterator const&);

#include <filesystem>
#include <type_traits>
#include <set>
#include <cassert>

#include "test_macros.h"
#include "filesystem_test_helper.h"
namespace fs = std::filesystem;
using namespace fs;

recursive_directory_iterator createInterestingIterator(const static_test_env &static_env)
    // Create an "interesting" iterator where all fields are
    // in a non-default state. The returned 'it' is in a
    // state such that:
    //   it.options() == directory_options::skip_permission_denied
    //   it.depth() == 1
    //   it.recursion_pending() == true
{
    const path testDir = static_env.Dir;
    const recursive_directory_iterator endIt;
    recursive_directory_iterator it(testDir,
                                    directory_options::skip_permission_denied);
    assert(it != endIt);
    while (it.depth() != 1) {
        ++it;
        assert(it != endIt);
    }
    assert(it.depth() == 1);
    it.disable_recursion_pending();
    return it;
}


recursive_directory_iterator createDifferentInterestingIterator(const static_test_env &static_env)
    // Create an "interesting" iterator where all fields are
    // in a non-default state. The returned 'it' is in a
    // state such that:
    //   it.options() == directory_options::follow_directory_symlink
    //   it.depth() == 2
    //   it.recursion_pending() == false
{
    const path testDir = static_env.Dir;
    const recursive_directory_iterator endIt;
    recursive_directory_iterator it(testDir,
                                    directory_options::follow_directory_symlink);
    assert(it != endIt);
    while (it.depth() != 2) {
        ++it;
        assert(it != endIt);
    }
    assert(it.depth() == 2);
    return it;
}

static void test_assignment_signature() {
    using D = recursive_directory_iterator;
    static_assert(std::is_copy_assignable<D>::value, "");
}

static void test_copy_to_end_iterator()
{
    static_test_env static_env;
    const recursive_directory_iterator endIt;

    const recursive_directory_iterator from = createInterestingIterator(static_env);
    const path entry = *from;

    recursive_directory_iterator to;
    to = from;
    assert(to == from);
    assert(*to == entry);
    assert(to.options() == from.options());
    assert(to.depth() == from.depth());
    assert(to.recursion_pending() == from.recursion_pending());
}


static void test_copy_from_end_iterator()
{
    static_test_env static_env;
    const recursive_directory_iterator from;
    recursive_directory_iterator to = createInterestingIterator(static_env);

    to = from;
    assert(to == from);
    assert(to == recursive_directory_iterator{});
}

static void test_copy_valid_iterator()
{
    static_test_env static_env;
    const recursive_directory_iterator endIt;

    const recursive_directory_iterator it = createInterestingIterator(static_env);
    const path entry = *it;

    recursive_directory_iterator it2 = createDifferentInterestingIterator(static_env);
    assert(it2                   != it);
    assert(it2.options()           != it.options());
    assert(it2.depth()             != it.depth());
    assert(it2.recursion_pending() != it.recursion_pending());
    assert(*it2                    != entry);

    it2 = it;
    assert(it2                   == it);
    assert(it2.options()           == it.options());
    assert(it2.depth()             == it.depth());
    assert(it2.recursion_pending() == it.recursion_pending());
    assert(*it2                    == entry);
}

static void test_returns_reference_to_self()
{
    const recursive_directory_iterator it;
    recursive_directory_iterator it2;
    recursive_directory_iterator& ref = (it2 = it);
    assert(&ref == &it2);
}

static void test_self_copy()
{
    static_test_env static_env;
    // Create two non-equal iterators that have exactly the same state.
    recursive_directory_iterator it = createInterestingIterator(static_env);
    recursive_directory_iterator it2 = createInterestingIterator(static_env);
    assert(it != it2);
    assert(it2.options()           == it.options());
    assert(it2.depth()             == it.depth());
    assert(it2.recursion_pending() == it.recursion_pending());
    assert(*it2 == *it);

    // perform a self-copy and check that the state still matches the
    // other unmodified iterator.
    recursive_directory_iterator const& cit = it;
    it = cit;
    assert(it2.options()           == it.options());
    assert(it2.depth()             == it.depth());
    assert(it2.recursion_pending() == it.recursion_pending());
    assert(*it2 == *it);
}

int main(int, char**) {
    test_assignment_signature();
    test_copy_to_end_iterator();
    test_copy_from_end_iterator();
    test_copy_valid_iterator();
    test_returns_reference_to_self();
    test_self_copy();

    return 0;
}