lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] What does this test do?


From: Greg Chicares
Subject: Re: [lmi] What does this test do?
Date: Tue, 11 May 2021 14:25:38 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0

On 5/9/21 9:11 PM, Vadim Zeitlin wrote:
> On Mon, 3 May 2021 14:50:05 +0000 Greg Chicares <gchicares@sbcglobal.net> 
> wrote:
> 
> GC> Vadim--in a file that hasn't changed substantially since its 2005 
> original,
> GC> I see:
> GC> 
> GC>     fs::directory_iterator const i(path);
> GC>     LMI_TEST(i->exists());
> GC> 
> GC> and wonder: what was I thinking?

Replaced, as proposed below, by
  LMI_TEST(fs::is_directory(path));
in a soon-to-be pushed commit.

> GC> I'd like to replace those lines with something I can understand, e.g.:
> GC>   LMI_TEST(is_directory(path));
> GC> Poring over the Standard leads me to believe is_directory() is
> GC> implemented in terms of status(), which behaves as if implemented
> GC> in terms of posix stat(). And we can't 'stat' a file that doesn't
> GC> exist, AFAIK, so if 'true == is_directory(path)', then the directory
> GC> necessarily exists. Therefore, it's not necessary to add an explicit
> GC> existence test:
> GC> 
> GC>     LMI_TEST(is_directory(path)); // Success implies existence.
> GC> //  LMI_TEST(path.exists());      // Unnecessary.

But let's consider the same lines, in opposite order:

    LMI_TEST(path.exists());      // #1
    LMI_TEST(is_directory(path)); // #2

Is it ever useful to test both those conditions, in that order?
Specifically, if #1 fails, then might #2 throw?

In my tests, it doesn't throw: i.e.,
  fs::is_directory("This_path_does_not_exist");
just returns 'false'. But AFAICT the standard says it calls
std::filesystem::status(), which can throw if the underlying OS
reports an error...so testing condition #1 before #2 might be
a good idea. IOW, is the dual test in this actual lmi line:

  LMI_TEST(fs::exists(remote_dir_0) && fs::is_directory(remote_dir_0));

useful, or wasteful?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]