| 
									
										
										
										
											2018-03-04 09:16:51 -08:00
										 |  |  | // Tests of the C++ interface to POSIX functions that require mocks
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Copyright (c) 2012 - present, Victor Zverovich
 | 
					
						
							|  |  |  | // All rights reserved.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // For the license information refer to format.h.
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-17 06:53:32 -08:00
										 |  |  | // Disable bogus MSVC warnings.
 | 
					
						
							| 
									
										
										
										
											2019-05-08 02:09:11 +07:00
										 |  |  | #ifndef _CRT_SECURE_NO_WARNINGS
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  define _CRT_SECURE_NO_WARNINGS
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:57:59 +03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-12-17 06:53:32 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 08:43:30 -07:00
										 |  |  | #include "posix-mock.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #include <climits>
 | 
					
						
							| 
									
										
										
										
											2019-01-02 10:03:04 -08:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  | #include "../src/os.cc"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  include <io.h>
 | 
					
						
							|  |  |  | #  undef max
 | 
					
						
							|  |  |  | #  undef ERROR
 | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:57:59 +03:00
										 |  |  | #include "gmock.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | #include "gtest-extra.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-24 09:16:03 -07:00
										 |  |  | #include "util.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-19 08:57:31 -07:00
										 |  |  | using fmt::buffered_file; | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  | using fmt::error_code; | 
					
						
							| 
									
										
										
										
											2014-06-09 09:07:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | using testing::_; | 
					
						
							|  |  |  | using testing::Return; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | using testing::StrEq; | 
					
						
							| 
									
										
										
										
											2015-07-31 08:24:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | namespace { | 
					
						
							|  |  |  | int open_count; | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | int close_count; | 
					
						
							|  |  |  | int dup_count; | 
					
						
							|  |  |  | int dup2_count; | 
					
						
							|  |  |  | int fdopen_count; | 
					
						
							|  |  |  | int read_count; | 
					
						
							|  |  |  | int write_count; | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  | int pipe_count; | 
					
						
							| 
									
										
										
										
											2014-08-28 11:53:05 -07:00
										 |  |  | int fopen_count; | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  | int fclose_count; | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  | int fileno_count; | 
					
						
							| 
									
										
										
										
											2020-05-07 15:59:46 -07:00
										 |  |  | size_t read_nbyte; | 
					
						
							|  |  |  | size_t write_nbyte; | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  | bool sysconf_error; | 
					
						
							| 
									
										
										
										
											2014-09-12 16:52:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  | enum { NONE, MAX_SIZE, ERROR } fstat_sim; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | }  // namespace
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | #define EMULATE_EINTR(func, error_result) \
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   if (func##_count != 0) {                \ | 
					
						
							|  |  |  |     if (func##_count++ != 3) {            \ | 
					
						
							|  |  |  |       errno = EINTR;                      \ | 
					
						
							|  |  |  |       return error_result;                \ | 
					
						
							|  |  |  |     }                                     \ | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 07:29:58 -07:00
										 |  |  | #ifndef _MSC_VER
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | int test::open(const char* path, int oflag, int mode) { | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  |   EMULATE_EINTR(open, -1); | 
					
						
							|  |  |  |   return ::open(path, oflag, mode); | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-06 07:29:58 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | errno_t test::sopen_s(int* pfh, const char* filename, int oflag, int shflag, | 
					
						
							|  |  |  |                       int pmode) { | 
					
						
							| 
									
										
										
										
											2015-05-06 07:29:58 -07:00
										 |  |  |   EMULATE_EINTR(open, EINTR); | 
					
						
							|  |  |  |   return _sopen_s(pfh, filename, oflag, shflag, pmode); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 07:29:58 -07:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2015-05-11 19:10:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  | long test::sysconf(int name) { | 
					
						
							|  |  |  |   long result = ::sysconf(name); | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   if (!sysconf_error) return result; | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  |   // Simulate an error.
 | 
					
						
							|  |  |  |   errno = EINVAL; | 
					
						
							|  |  |  |   return -1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-11 19:10:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | static off_t max_file_size() { return std::numeric_limits<off_t>::max(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | int test::fstat(int fd, struct stat* buf) { | 
					
						
							| 
									
										
										
										
											2015-05-11 19:10:31 -07:00
										 |  |  |   int result = ::fstat(fd, buf); | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   if (fstat_sim == MAX_SIZE) buf->st_size = max_file_size(); | 
					
						
							| 
									
										
										
										
											2015-05-11 19:10:31 -07:00
										 |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-05-11 19:10:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | static LONGLONG max_file_size() { return std::numeric_limits<LONGLONG>::max(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 08:52:23 -07:00
										 |  |  | DWORD test::GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) { | 
					
						
							| 
									
										
										
										
											2014-09-12 16:52:15 -07:00
										 |  |  |   if (fstat_sim == ERROR) { | 
					
						
							|  |  |  |     SetLastError(ERROR_ACCESS_DENIED); | 
					
						
							| 
									
										
										
										
											2015-03-16 09:17:18 -07:00
										 |  |  |     return INVALID_FILE_SIZE; | 
					
						
							| 
									
										
										
										
											2014-09-12 16:52:15 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-03-16 08:52:23 -07:00
										 |  |  |   if (fstat_sim == MAX_SIZE) { | 
					
						
							|  |  |  |     DWORD max = std::numeric_limits<DWORD>::max(); | 
					
						
							| 
									
										
										
										
											2015-03-16 09:05:46 -07:00
										 |  |  |     *lpFileSizeHigh = max >> 1; | 
					
						
							| 
									
										
										
										
											2015-03-16 08:52:23 -07:00
										 |  |  |     return max; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return ::GetFileSize(hFile, lpFileSizeHigh); | 
					
						
							| 
									
										
										
										
											2014-09-12 11:12:22 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-11 19:10:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | int test::close(int fildes) { | 
					
						
							|  |  |  |   // Close the file first because close shouldn't be retried.
 | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   int result = ::FMT_POSIX(close(fildes)); | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  |   EMULATE_EINTR(close, -1); | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int test::dup(int fildes) { | 
					
						
							|  |  |  |   EMULATE_EINTR(dup, -1); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   return ::FMT_POSIX(dup(fildes)); | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int test::dup2(int fildes, int fildes2) { | 
					
						
							|  |  |  |   EMULATE_EINTR(dup2, -1); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   return ::FMT_POSIX(dup2(fildes, fildes2)); | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | FILE* test::fdopen(int fildes, const char* mode) { | 
					
						
							| 
									
										
										
										
											2019-05-30 07:01:31 -07:00
										 |  |  |   EMULATE_EINTR(fdopen, nullptr); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   return ::FMT_POSIX(fdopen(fildes, mode)); | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | test::ssize_t test::read(int fildes, void* buf, test::size_t nbyte) { | 
					
						
							| 
									
										
										
										
											2014-05-19 08:25:08 -07:00
										 |  |  |   read_nbyte = nbyte; | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  |   EMULATE_EINTR(read, -1); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   return ::FMT_POSIX(read(fildes, buf, nbyte)); | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | test::ssize_t test::write(int fildes, const void* buf, test::size_t nbyte) { | 
					
						
							| 
									
										
										
										
											2014-05-19 08:37:28 -07:00
										 |  |  |   write_nbyte = nbyte; | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  |   EMULATE_EINTR(write, -1); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   return ::FMT_POSIX(write(fildes, buf, nbyte)); | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  | int test::pipe(int fildes[2]) { | 
					
						
							|  |  |  |   EMULATE_EINTR(pipe, -1); | 
					
						
							|  |  |  |   return ::pipe(fildes); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | int test::pipe(int* pfds, unsigned psize, int textmode) { | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  |   EMULATE_EINTR(pipe, -1); | 
					
						
							| 
									
										
										
										
											2014-05-18 12:38:01 -07:00
										 |  |  |   return _pipe(pfds, psize, textmode); | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | FILE* test::fopen(const char* filename, const char* mode) { | 
					
						
							| 
									
										
										
										
											2019-05-30 07:01:31 -07:00
										 |  |  |   EMULATE_EINTR(fopen, nullptr); | 
					
						
							| 
									
										
										
										
											2014-08-28 11:53:05 -07:00
										 |  |  |   return ::fopen(filename, mode); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | int test::fclose(FILE* stream) { | 
					
						
							| 
									
										
										
										
											2014-05-18 11:09:37 -07:00
										 |  |  |   EMULATE_EINTR(fclose, EOF); | 
					
						
							|  |  |  |   return ::fclose(stream); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | int(test::fileno)(FILE* stream) { | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   EMULATE_EINTR(fileno, -1); | 
					
						
							| 
									
										
										
										
											2015-05-06 07:19:42 -07:00
										 |  |  | #ifdef fileno
 | 
					
						
							|  |  |  |   return FMT_POSIX(fileno(stream)); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2014-05-19 07:53:16 -07:00
										 |  |  |   return ::FMT_POSIX(fileno(stream)); | 
					
						
							| 
									
										
										
										
											2015-05-06 07:19:42 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  define EXPECT_RETRY(statement, func, message) \
 | 
					
						
							|  |  |  |     func##_count = 1;                            \ | 
					
						
							|  |  |  |     statement;                                   \ | 
					
						
							|  |  |  |     EXPECT_EQ(4, func##_count);                  \ | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  |     func##_count = 0; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  define EXPECT_EQ_POSIX(expected, actual) EXPECT_EQ(expected, actual)
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  define EXPECT_RETRY(statement, func, message)    \
 | 
					
						
							|  |  |  |     func##_count = 1;                               \ | 
					
						
							| 
									
										
										
										
											2014-05-15 08:58:10 -07:00
										 |  |  |     EXPECT_SYSTEM_ERROR(statement, EINTR, message); \ | 
					
						
							|  |  |  |     func##_count = 0; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  define EXPECT_EQ_POSIX(expected, actual)
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:57:59 +03:00
										 |  |  | static void write_file(fmt::cstring_view filename, fmt::string_view content) { | 
					
						
							| 
									
										
										
										
											2018-05-19 08:57:31 -07:00
										 |  |  |   fmt::buffered_file f(filename, "w"); | 
					
						
							| 
									
										
										
										
											2014-09-12 12:05:32 -07:00
										 |  |  |   f.print("{}", content); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 05:41:14 -08:00
										 |  |  | #if FMT_USE_FCNTL
 | 
					
						
							|  |  |  | using fmt::file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  | TEST(UtilTest, GetPageSize) { | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  |   SYSTEM_INFO si = {}; | 
					
						
							|  |  |  |   GetSystemInfo(&si); | 
					
						
							|  |  |  |   EXPECT_EQ(si.dwPageSize, fmt::getpagesize()); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  else
 | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  |   EXPECT_EQ(sysconf(_SC_PAGESIZE), fmt::getpagesize()); | 
					
						
							|  |  |  |   sysconf_error = true; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   EXPECT_SYSTEM_ERROR(fmt::getpagesize(), EINVAL, | 
					
						
							|  |  |  |                       "cannot get memory page size"); | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  |   sysconf_error = false; | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2014-09-12 13:53:52 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | TEST(FileTest, OpenRetry) { | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   write_file("temp", "there must be something here"); | 
					
						
							| 
									
										
										
										
											2019-05-30 07:01:31 -07:00
										 |  |  |   std::unique_ptr<file> f{nullptr}; | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open, | 
					
						
							|  |  |  |                "cannot open file temp"); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  |   char c = 0; | 
					
						
							|  |  |  |   f->read(&c, 1); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2014-05-15 07:45:44 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 06:25:20 -07:00
										 |  |  | TEST(FileTest, CloseNoRetryInDtor) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2019-01-02 10:03:04 -08:00
										 |  |  |   std::unique_ptr<file> f(new file(std::move(read_end))); | 
					
						
							| 
									
										
										
										
											2014-05-16 06:25:20 -07:00
										 |  |  |   int saved_close_count = 0; | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   EXPECT_WRITE( | 
					
						
							|  |  |  |       stderr, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         close_count = 1; | 
					
						
							|  |  |  |         f.reset(nullptr); | 
					
						
							|  |  |  |         saved_close_count = close_count; | 
					
						
							|  |  |  |         close_count = 0; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       format_system_error(EINTR, "cannot close file") + "\n"); | 
					
						
							| 
									
										
										
										
											2014-05-16 06:25:20 -07:00
										 |  |  |   EXPECT_EQ(2, saved_close_count); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(FileTest, CloseNoRetry) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2014-05-16 06:25:20 -07:00
										 |  |  |   close_count = 1; | 
					
						
							|  |  |  |   EXPECT_SYSTEM_ERROR(read_end.close(), EINTR, "cannot close file"); | 
					
						
							|  |  |  |   EXPECT_EQ(2, close_count); | 
					
						
							|  |  |  |   close_count = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-12 10:50:16 -07:00
										 |  |  | TEST(FileTest, Size) { | 
					
						
							| 
									
										
										
										
											2014-09-12 12:05:32 -07:00
										 |  |  |   std::string content = "top secret, destroy before reading"; | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   write_file("temp", content); | 
					
						
							|  |  |  |   file f("temp", file::RDONLY); | 
					
						
							| 
									
										
										
										
											2014-10-13 08:39:38 -07:00
										 |  |  |   EXPECT_GE(f.size(), 0); | 
					
						
							| 
									
										
										
										
											2017-08-26 09:09:43 -07:00
										 |  |  |   EXPECT_EQ(content.size(), static_cast<unsigned long long>(f.size())); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2017-02-19 06:46:51 -08:00
										 |  |  |   fmt::memory_buffer message; | 
					
						
							| 
									
										
										
										
											2020-05-10 07:25:42 -07:00
										 |  |  |   fmt::detail::format_windows_error(message, ERROR_ACCESS_DENIED, | 
					
						
							|  |  |  |                                     "cannot get file size"); | 
					
						
							| 
									
										
										
										
											2014-09-12 16:52:15 -07:00
										 |  |  |   fstat_sim = ERROR; | 
					
						
							| 
									
										
										
										
											2017-03-04 07:10:54 -08:00
										 |  |  |   EXPECT_THROW_MSG(f.size(), fmt::windows_error, fmt::to_string(message)); | 
					
						
							| 
									
										
										
										
											2014-09-12 17:16:22 -07:00
										 |  |  |   fstat_sim = NONE; | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  else
 | 
					
						
							| 
									
										
										
										
											2014-09-12 18:22:45 -07:00
										 |  |  |   f.close(); | 
					
						
							| 
									
										
										
										
											2014-09-12 11:12:22 -07:00
										 |  |  |   EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes"); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2014-09-12 10:50:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-12 11:12:22 -07:00
										 |  |  | TEST(FileTest, MaxSize) { | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   write_file("temp", ""); | 
					
						
							|  |  |  |   file f("temp", file::RDONLY); | 
					
						
							| 
									
										
										
										
											2014-09-12 16:52:15 -07:00
										 |  |  |   fstat_sim = MAX_SIZE; | 
					
						
							| 
									
										
										
										
											2014-09-12 11:12:22 -07:00
										 |  |  |   EXPECT_GE(f.size(), 0); | 
					
						
							|  |  |  |   EXPECT_EQ(max_file_size(), f.size()); | 
					
						
							| 
									
										
										
										
											2014-09-12 16:52:15 -07:00
										 |  |  |   fstat_sim = NONE; | 
					
						
							| 
									
										
										
										
											2014-09-12 11:12:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  | TEST(FileTest, ReadRetry) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  |   enum { SIZE = 4 }; | 
					
						
							|  |  |  |   write_end.write("test", SIZE); | 
					
						
							|  |  |  |   write_end.close(); | 
					
						
							|  |  |  |   char buffer[SIZE]; | 
					
						
							| 
									
										
										
										
											2020-05-07 15:59:46 -07:00
										 |  |  |   size_t count = 0; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   EXPECT_RETRY(count = read_end.read(buffer, SIZE), read, | 
					
						
							|  |  |  |                "cannot read from file"); | 
					
						
							| 
									
										
										
										
											2014-06-07 07:11:34 -07:00
										 |  |  |   EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count); | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(FileTest, WriteRetry) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  |   enum { SIZE = 4 }; | 
					
						
							| 
									
										
										
										
											2020-05-07 15:59:46 -07:00
										 |  |  |   size_t count = 0; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   EXPECT_RETRY(count = write_end.write("test", SIZE), write, | 
					
						
							|  |  |  |                "cannot write to file"); | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  |   write_end.close(); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-06-07 07:11:34 -07:00
										 |  |  |   EXPECT_EQ(static_cast<std::streamsize>(SIZE), count); | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  |   char buffer[SIZE + 1]; | 
					
						
							|  |  |  |   read_end.read(buffer, SIZE); | 
					
						
							|  |  |  |   buffer[SIZE] = '\0'; | 
					
						
							|  |  |  |   EXPECT_STREQ("test", buffer); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-05-19 08:25:08 -07:00
										 |  |  | TEST(FileTest, ConvertReadCount) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2014-05-19 08:25:08 -07:00
										 |  |  |   char c; | 
					
						
							| 
									
										
										
										
											2020-05-07 15:59:46 -07:00
										 |  |  |   size_t size = UINT_MAX; | 
					
						
							|  |  |  |   if (sizeof(unsigned) != sizeof(size_t)) ++size; | 
					
						
							| 
									
										
										
										
											2014-05-19 08:25:08 -07:00
										 |  |  |   read_count = 1; | 
					
						
							| 
									
										
										
										
											2014-05-19 08:37:28 -07:00
										 |  |  |   read_nbyte = 0; | 
					
						
							| 
									
										
										
										
											2017-03-04 07:10:54 -08:00
										 |  |  |   EXPECT_THROW(read_end.read(&c, size), fmt::system_error); | 
					
						
							| 
									
										
										
										
											2014-05-19 08:25:08 -07:00
										 |  |  |   read_count = 0; | 
					
						
							|  |  |  |   EXPECT_EQ(UINT_MAX, read_nbyte); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-19 08:37:28 -07:00
										 |  |  | TEST(FileTest, ConvertWriteCount) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2014-05-19 08:37:28 -07:00
										 |  |  |   char c; | 
					
						
							| 
									
										
										
										
											2020-05-07 15:59:46 -07:00
										 |  |  |   size_t size = UINT_MAX; | 
					
						
							|  |  |  |   if (sizeof(unsigned) != sizeof(size_t)) ++size; | 
					
						
							| 
									
										
										
										
											2014-05-19 08:37:28 -07:00
										 |  |  |   write_count = 1; | 
					
						
							|  |  |  |   write_nbyte = 0; | 
					
						
							| 
									
										
										
										
											2017-03-04 07:10:54 -08:00
										 |  |  |   EXPECT_THROW(write_end.write(&c, size), fmt::system_error); | 
					
						
							| 
									
										
										
										
											2014-05-19 08:37:28 -07:00
										 |  |  |   write_count = 0; | 
					
						
							|  |  |  |   EXPECT_EQ(UINT_MAX, write_nbyte); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2014-05-19 08:25:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  | TEST(FileTest, DupNoRetry) { | 
					
						
							| 
									
										
										
										
											2014-05-19 07:53:16 -07:00
										 |  |  |   int stdout_fd = FMT_POSIX(fileno(stdout)); | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   dup_count = 1; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   EXPECT_SYSTEM_ERROR( | 
					
						
							|  |  |  |       file::dup(stdout_fd), EINTR, | 
					
						
							| 
									
										
										
										
											2014-06-29 11:51:10 -07:00
										 |  |  |       fmt::format("cannot duplicate file descriptor {}", stdout_fd)); | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   dup_count = 0; | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(FileTest, Dup2Retry) { | 
					
						
							| 
									
										
										
										
											2014-05-19 07:53:16 -07:00
										 |  |  |   int stdout_fd = FMT_POSIX(fileno(stdout)); | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd); | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  |   EXPECT_RETRY(f1.dup2(f2.descriptor()), dup2, | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |                fmt::format("cannot duplicate file descriptor {} to {}", | 
					
						
							|  |  |  |                            f1.descriptor(), f2.descriptor())); | 
					
						
							| 
									
										
										
										
											2014-05-16 07:07:43 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  | TEST(FileTest, Dup2NoExceptRetry) { | 
					
						
							| 
									
										
										
										
											2014-05-19 07:53:16 -07:00
										 |  |  |   int stdout_fd = FMT_POSIX(fileno(stdout)); | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd); | 
					
						
							|  |  |  |   error_code ec; | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   dup2_count = 1; | 
					
						
							|  |  |  |   f1.dup2(f2.descriptor(), ec); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   EXPECT_EQ(4, dup2_count); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  else
 | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   EXPECT_EQ(EINTR, ec.get()); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   dup2_count = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(FileTest, PipeNoRetry) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   pipe_count = 1; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   EXPECT_SYSTEM_ERROR(file::pipe(read_end, write_end), EINTR, | 
					
						
							|  |  |  |                       "cannot create pipe"); | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   pipe_count = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(FileTest, FdopenNoRetry) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   fdopen_count = 1; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   EXPECT_SYSTEM_ERROR(read_end.fdopen("r"), EINTR, | 
					
						
							|  |  |  |                       "cannot associate stream with file descriptor"); | 
					
						
							| 
									
										
										
										
											2014-05-18 10:05:29 -07:00
										 |  |  |   fdopen_count = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 11:53:05 -07:00
										 |  |  | TEST(BufferedFileTest, OpenRetry) { | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   write_file("temp", "there must be something here"); | 
					
						
							| 
									
										
										
										
											2019-05-30 07:01:31 -07:00
										 |  |  |   std::unique_ptr<buffered_file> f{nullptr}; | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   EXPECT_RETRY(f.reset(new buffered_file("temp", "r")), fopen, | 
					
						
							|  |  |  |                "cannot open file temp"); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-08-28 11:53:05 -07:00
										 |  |  |   char c = 0; | 
					
						
							| 
									
										
										
										
											2014-08-29 06:57:26 -07:00
										 |  |  |   if (fread(&c, 1, 1, f->get()) < 1) | 
					
						
							| 
									
										
										
										
											2017-02-19 08:41:38 -08:00
										 |  |  |     throw fmt::system_error(errno, "fread failed"); | 
					
						
							| 
									
										
										
										
											2019-12-21 13:10:45 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2014-08-28 11:53:05 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  | TEST(BufferedFileTest, CloseNoRetryInDtor) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2019-01-02 10:03:04 -08:00
										 |  |  |   std::unique_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r"))); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   int saved_fclose_count = 0; | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   EXPECT_WRITE( | 
					
						
							|  |  |  |       stderr, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         fclose_count = 1; | 
					
						
							|  |  |  |         f.reset(nullptr); | 
					
						
							|  |  |  |         saved_fclose_count = fclose_count; | 
					
						
							|  |  |  |         fclose_count = 0; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       format_system_error(EINTR, "cannot close file") + "\n"); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   EXPECT_EQ(2, saved_fclose_count); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(BufferedFileTest, CloseNoRetry) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2018-05-19 08:57:31 -07:00
										 |  |  |   buffered_file f = read_end.fdopen("r"); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   fclose_count = 1; | 
					
						
							|  |  |  |   EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file"); | 
					
						
							|  |  |  |   EXPECT_EQ(2, fclose_count); | 
					
						
							|  |  |  |   fclose_count = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(BufferedFileTest, FilenoNoRetry) { | 
					
						
							| 
									
										
										
										
											2018-05-19 10:32:53 -07:00
										 |  |  |   file read_end, write_end; | 
					
						
							|  |  |  |   file::pipe(read_end, write_end); | 
					
						
							| 
									
										
										
										
											2018-05-19 08:57:31 -07:00
										 |  |  |   buffered_file f = read_end.fdopen("r"); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   fileno_count = 1; | 
					
						
							| 
									
										
										
										
											2015-05-06 07:19:42 -07:00
										 |  |  |   EXPECT_SYSTEM_ERROR((f.fileno)(), EINTR, "cannot get file descriptor"); | 
					
						
							| 
									
										
										
										
											2014-05-19 07:31:05 -07:00
										 |  |  |   EXPECT_EQ(2, fileno_count); | 
					
						
							|  |  |  |   fileno_count = 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-15 05:41:14 -08:00
										 |  |  | #endif  // FMT_USE_FCNTL
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  | struct test_mock { | 
					
						
							|  |  |  |   static test_mock* instance; | 
					
						
							|  |  |  | } * test_mock::instance; | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST(ScopedMock, Scope) { | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |     ScopedMock<test_mock> mock; | 
					
						
							|  |  |  |     EXPECT_EQ(&mock, test_mock::instance); | 
					
						
							|  |  |  |     test_mock& copy = mock; | 
					
						
							| 
									
										
										
										
											2018-06-06 16:57:59 +03:00
										 |  |  |     static_cast<void>(copy); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   EXPECT_EQ(nullptr, test_mock::instance); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef FMT_LOCALE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  | typedef fmt::locale::type locale_type; | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  | struct locale_mock { | 
					
						
							|  |  |  |   static locale_mock* instance; | 
					
						
							|  |  |  |   MOCK_METHOD3(newlocale, locale_type(int category_mask, const char* locale, | 
					
						
							|  |  |  |                                       locale_type base)); | 
					
						
							|  |  |  |   MOCK_METHOD1(freelocale, void(locale_type locale)); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   MOCK_METHOD3(strtod_l, | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |                double(const char* nptr, char** endptr, locale_type locale)); | 
					
						
							|  |  |  | } * locale_mock::instance; | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  ifdef _MSC_VER
 | 
					
						
							|  |  |  | #    pragma warning(push)
 | 
					
						
							|  |  |  | #    pragma warning(disable : 4273)
 | 
					
						
							| 
									
										
										
										
											2019-05-12 01:48:27 +07:00
										 |  |  | #    ifdef __clang__
 | 
					
						
							|  |  |  | #      pragma clang diagnostic push
 | 
					
						
							|  |  |  | #      pragma clang diagnostic ignored "-Winconsistent-dllimport"
 | 
					
						
							|  |  |  | #    endif
 | 
					
						
							| 
									
										
										
										
											2016-03-19 06:39:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | _locale_t _create_locale(int category, const char* locale) { | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   return locale_mock::instance->newlocale(category, locale, 0); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void _free_locale(_locale_t locale) { | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   locale_mock::instance->freelocale(locale); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | double _strtod_l(const char* nptr, char** endptr, _locale_t locale) { | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   return locale_mock::instance->strtod_l(nptr, endptr, locale); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-12 01:48:27 +07:00
										 |  |  | #    ifdef __clang__
 | 
					
						
							|  |  |  | #      pragma clang diagnostic pop
 | 
					
						
							|  |  |  | #    endif
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #    pragma warning(pop)
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  if defined(__THROW) && FMT_GCC_VERSION > 0 && FMT_GCC_VERSION <= 408
 | 
					
						
							|  |  |  | #    define FMT_LOCALE_THROW __THROW
 | 
					
						
							|  |  |  | #  else
 | 
					
						
							|  |  |  | #    define FMT_LOCALE_THROW
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:57:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  if defined(__APPLE__) || \
 | 
					
						
							|  |  |  |       (defined(__FreeBSD__) && __FreeBSD_version < 1200002) | 
					
						
							| 
									
										
										
										
											2016-02-02 22:06:54 -08:00
										 |  |  | typedef int FreeLocaleResult; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  else
 | 
					
						
							| 
									
										
										
										
											2016-02-02 22:06:54 -08:00
										 |  |  | typedef void FreeLocaleResult; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  | FreeLocaleResult freelocale(locale_type locale) FMT_LOCALE_THROW { | 
					
						
							|  |  |  |   locale_mock::instance->freelocale(locale); | 
					
						
							| 
									
										
										
										
											2016-02-11 06:55:53 -08:00
										 |  |  |   return FreeLocaleResult(); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | double strtod_l(const char* nptr, char** endptr, | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |                 locale_type locale) FMT_LOCALE_THROW { | 
					
						
							|  |  |  |   return locale_mock::instance->strtod_l(nptr, endptr, locale); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  undef FMT_LOCALE_THROW
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:57:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  | #  ifndef _WIN32
 | 
					
						
							|  |  |  | locale_t test::newlocale(int category_mask, const char* locale, locale_t base) { | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   return locale_mock::instance->newlocale(category_mask, locale, base); | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | TEST(LocaleTest, LocaleMock) { | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   ScopedMock<locale_mock> mock; | 
					
						
							|  |  |  |   locale_type locale = reinterpret_cast<locale_type>(11); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |   EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale)); | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  |   FMT_SYSTEM(newlocale(222, "foo", locale)); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-01 08:42:14 -07:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST(LocaleTest, Locale) { | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  ifndef LC_NUMERIC_MASK
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |   enum { LC_NUMERIC_MASK = LC_NUMERIC }; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   ScopedMock<locale_mock> mock; | 
					
						
							|  |  |  |   locale_type impl = reinterpret_cast<locale_type>(42); | 
					
						
							| 
									
										
										
										
											2019-05-30 07:01:31 -07:00
										 |  |  |   EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), nullptr)) | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |       .WillOnce(Return(impl)); | 
					
						
							|  |  |  |   EXPECT_CALL(mock, freelocale(impl)); | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   fmt::locale loc; | 
					
						
							|  |  |  |   EXPECT_EQ(impl, loc.get()); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(LocaleTest, Strtod) { | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   ScopedMock<locale_mock> mock; | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |   EXPECT_CALL(mock, newlocale(_, _, _)) | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |       .WillOnce(Return(reinterpret_cast<locale_type>(42))); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |   EXPECT_CALL(mock, freelocale(_)); | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   fmt::locale loc; | 
					
						
							| 
									
										
										
										
											2019-01-12 18:27:38 -08:00
										 |  |  |   const char* str = "4.2"; | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |   char end = 'x'; | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   EXPECT_CALL(mock, strtod_l(str, _, loc.get())) | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |       .WillOnce(testing::DoAll(testing::SetArgPointee<1>(&end), Return(777))); | 
					
						
							| 
									
										
										
										
											2020-04-01 09:30:28 -07:00
										 |  |  |   EXPECT_EQ(777, loc.strtod(str)); | 
					
						
							| 
									
										
										
										
											2016-02-02 17:21:09 -08:00
										 |  |  |   EXPECT_EQ(&end, str); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif  // FMT_LOCALE
 |