• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright (c) 2020 Alexander Grund
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See
5 //  accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #include <boost/nowide/stat.hpp>
9 
10 #include <boost/nowide/cstdio.hpp>
11 #ifdef BOOST_WINDOWS
12 #include <errno.h>
13 #endif
14 
15 #include "test.hpp"
16 
test_main(int,char ** argv,char **)17 void test_main(int, char** argv, char**)
18 {
19     const std::string prefix = argv[0];
20     const std::string filename = prefix + "\xd7\xa9-\xd0\xbc-\xce\xbd.txt";
21 
22     // Make sure file does not exist
23     boost::nowide::remove(filename.c_str());
24 
25     std::cout << " -- stat - non-existing file" << std::endl;
26     {
27 #ifdef BOOST_WINDOWS
28         struct _stat stdStat;
29 #else
30         struct stat stdStat;
31 #endif
32         TEST(boost::nowide::stat(filename.c_str(), &stdStat) != 0);
33         boost::nowide::stat_t boostStat;
34         TEST(boost::nowide::stat(filename.c_str(), &boostStat) != 0);
35     }
36 
37     std::cout << " -- stat - existing file" << std::endl;
38     FILE* f = boost::nowide::fopen(filename.c_str(), "wb");
39     TEST(f);
40     const char testData[] = "Hello World";
41     constexpr size_t testDataSize = sizeof(testData);
42     TEST(std::fwrite(testData, sizeof(char), testDataSize, f) == testDataSize);
43     std::fclose(f);
44     {
45 #ifdef BOOST_WINDOWS
46         struct _stat stdStat;
47 #else
48         struct stat stdStat;
49 #endif /*  */
50         TEST(boost::nowide::stat(filename.c_str(), &stdStat) == 0);
51         TEST(stdStat.st_size == testDataSize);
52         boost::nowide::stat_t boostStat;
53         TEST(boost::nowide::stat(filename.c_str(), &boostStat) == 0);
54         TEST(boostStat.st_size == testDataSize);
55     }
56 
57 #ifdef BOOST_WINDOWS
58     std::cout << " -- stat - invalid struct size" << std::endl;
59     {
60         struct _stat stdStat;
61         // Simulate passing a struct that is 4 bytes smaller, e.g. if it uses 32 bit time field instead of 64 bit
62         // Need to use the detail function directly
63         TEST(boost::nowide::detail::stat(filename.c_str(), &stdStat, sizeof(stdStat) - 4u) == EINVAL);
64         TEST(errno == EINVAL);
65     }
66 #endif
67 
68     boost::nowide::remove(filename.c_str());
69 }
70