@@ -5636,6 +5636,7 @@ def test_rename_silly(self):
5636
5636
5637
5637
def test_readdir_r_silly(self):
5638
5638
create_file('src.cpp', r'''
5639
+ #include <cassert>
5639
5640
#include <iostream>
5640
5641
#include <cstring>
5641
5642
#include <cerrno>
@@ -5645,29 +5646,23 @@ def test_readdir_r_silly(self):
5645
5646
#include <dirent.h>
5646
5647
#include <sys/stat.h>
5647
5648
#include <sys/types.h>
5649
+
5648
5650
using std::endl;
5651
+
5649
5652
namespace
5650
5653
{
5651
- void check(const bool result)
5652
- {
5653
- if(not result) {
5654
- std::cout << "Check failed!" << endl;
5655
- throw "bad";
5656
- }
5657
- }
5658
5654
// Do a recursive directory listing of the directory whose path is specified
5659
5655
// by \a name.
5660
- void ls(const std::string& name, std::size_t indent = 0)
5661
- {
5662
- ::DIR *dir;
5663
- struct ::dirent *entry;
5664
- if(indent == 0) {
5656
+ void ls(const std::string& name, size_t indent = 0) {
5657
+ DIR *dir;
5658
+ struct dirent *entry;
5659
+ if (indent == 0) {
5665
5660
std::cout << name << endl;
5666
5661
++indent;
5667
5662
}
5668
5663
// Make sure we can open the directory. This should also catch cases where
5669
5664
// the empty string is passed in.
5670
- if (not (dir = :: opendir(name.c_str()))) {
5665
+ if (not (dir = opendir(name.c_str()))) {
5671
5666
const int error = errno;
5672
5667
std::cout
5673
5668
<< "Failed to open directory: " << name << "; " << error << endl;
@@ -5678,17 +5673,17 @@ def test_readdir_r_silly(self):
5678
5673
std::cout
5679
5674
<< "Managed to open a directory whose name was the empty string.."
5680
5675
<< endl;
5681
- check(:: closedir(dir) != -1);
5676
+ assert( closedir(dir) != -1);
5682
5677
return;
5683
5678
}
5684
5679
// Iterate over the entries in the directory.
5685
- while ((entry = :: readdir(dir))) {
5686
- const std::string entryName(entry->d_name);
5680
+ while ((entry = readdir(dir))) {
5681
+ std::string entryName(entry->d_name);
5687
5682
if (entryName == "." || entryName == "..") {
5688
5683
// Skip the dot entries.
5689
5684
continue;
5690
5685
}
5691
- const std::string indentStr(indent * 2, ' ');
5686
+ std::string indentStr(indent * 2, ' ');
5692
5687
if (entryName.empty()) {
5693
5688
std::cout
5694
5689
<< indentStr << "\"\": Found empty string as a "
@@ -5706,18 +5701,18 @@ def test_readdir_r_silly(self):
5706
5701
}
5707
5702
}
5708
5703
// Close our handle.
5709
- check(:: closedir(dir) != -1);
5704
+ assert( closedir(dir) != -1);
5710
5705
}
5711
- void touch(const std::string &path)
5712
- {
5713
- const int fd = :: open(path.c_str() , O_CREAT | O_TRUNC, 0644);
5714
- check (fd != -1);
5715
- check(:: close(fd) != -1);
5706
+
5707
+ void touch(const char* path) {
5708
+ int fd = open(path, O_CREAT | O_TRUNC, 0644);
5709
+ assert (fd != -1);
5710
+ assert( close(fd) != -1);
5716
5711
}
5717
5712
}
5718
- int main()
5719
- {
5720
- check(:: mkdir("dir", 0755) == 0);
5713
+
5714
+ int main() {
5715
+ assert( mkdir("dir", 0755) == 0);
5721
5716
touch("dir/a");
5722
5717
touch("dir/b");
5723
5718
touch("dir/c");
@@ -5727,26 +5722,25 @@ def test_readdir_r_silly(self):
5727
5722
ls("dir");
5728
5723
std::cout << endl;
5729
5724
// Attempt to delete entries as we walk the (single) directory.
5730
- :: DIR * const dir = :: opendir("dir");
5731
- check (dir != NULL);
5732
- struct :: dirent *entry;
5733
- while((entry = :: readdir(dir)) != NULL) {
5734
- const std::string name(entry->d_name);
5725
+ DIR* dir = opendir("dir");
5726
+ assert (dir != NULL);
5727
+ struct dirent *entry;
5728
+ while ((entry = readdir(dir)) != NULL) {
5729
+ std::string name(entry->d_name);
5735
5730
// Skip "." and "..".
5736
- if(name == "." || name == "..") {
5731
+ if (name == "." || name == "..") {
5737
5732
continue;
5738
5733
}
5739
5734
// Unlink it.
5740
5735
std::cout << "Unlinking " << name << endl;
5741
- check(:: unlink(("dir/" + name).c_str()) != -1);
5736
+ assert( unlink(("dir/" + name).c_str()) != -1);
5742
5737
}
5743
- check(:: closedir(dir) != -1);
5738
+ assert( closedir(dir) != -1);
5744
5739
std::cout << "After:" << endl;
5745
5740
ls("dir");
5746
- std::cout << endl;
5741
+ std::cout << "done" << endl;
5747
5742
return 0;
5748
- }
5749
- ''')
5743
+ }''')
5750
5744
# cannot symlink nonexistents
5751
5745
self.do_runf('src.cpp', r'''Before:
5752
5746
dir
@@ -5763,6 +5757,7 @@ def test_readdir_r_silly(self):
5763
5757
Unlinking e
5764
5758
After:
5765
5759
dir
5760
+ done
5766
5761
''', args=['', 'abc'])
5767
5762
5768
5763
def test_emversion(self):
0 commit comments