Home | History | Annotate | Download | only in test
      1 /*
      2  Test utilities.
      3 
      4  Copyright (c) 2012-2014, Victor Zverovich
      5  All rights reserved.
      6 
      7  Redistribution and use in source and binary forms, with or without
      8  modification, are permitted provided that the following conditions are met:
      9 
     10  1. Redistributions of source code must retain the above copyright notice, this
     11     list of conditions and the following disclaimer.
     12  2. Redistributions in binary form must reproduce the above copyright notice,
     13     this list of conditions and the following disclaimer in the documentation
     14     and/or other materials provided with the distribution.
     15 
     16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
     20  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <cstdarg>
     29 #include <cstdio>
     30 #include <string>
     31 
     32 #include "fmt/posix.h"
     33 
     34 enum {BUFFER_SIZE = 256};
     35 
     36 #ifdef _MSC_VER
     37 # define FMT_VSNPRINTF vsprintf_s
     38 #else
     39 # define FMT_VSNPRINTF vsnprintf
     40 #endif
     41 
     42 template <std::size_t SIZE>
     43 void safe_sprintf(char (&buffer)[SIZE], const char *format, ...) {
     44   std::va_list args;
     45   va_start(args, format);
     46   FMT_VSNPRINTF(buffer, SIZE, format, args);
     47   va_end(args);
     48 }
     49 
     50 // Increment a number in a string.
     51 void increment(char *s);
     52 
     53 std::string get_system_error(int error_code);
     54 
     55 extern const char *const FILE_CONTENT;
     56 
     57 // Opens a buffered file for reading.
     58 fmt::BufferedFile open_buffered_file(FILE **fp = 0);
     59 
     60 inline FILE *safe_fopen(const char *filename, const char *mode) {
     61 #if defined(_WIN32) && !defined(__MINGW32__)
     62   // Fix MSVC warning about "unsafe" fopen.
     63   FILE *f = 0;
     64   errno = fopen_s(&f, filename, mode);
     65   return f;
     66 #else
     67   return std::fopen(filename, mode);
     68 #endif
     69 }
     70 
     71 template <typename Char>
     72 class BasicTestString {
     73  private:
     74   std::basic_string<Char> value_;
     75 
     76   static const Char EMPTY[];
     77 
     78  public:
     79   explicit BasicTestString(const Char *value = EMPTY) : value_(value) {}
     80 
     81   const std::basic_string<Char> &value() const { return value_; }
     82 };
     83 
     84 template <typename Char>
     85 const Char BasicTestString<Char>::EMPTY[] = {0};
     86 
     87 typedef BasicTestString<char> TestString;
     88 typedef BasicTestString<wchar_t> TestWString;
     89 
     90 template <typename Char>
     91 std::basic_ostream<Char> &operator<<(
     92     std::basic_ostream<Char> &os, const BasicTestString<Char> &s) {
     93   os << s.value();
     94   return os;
     95 }
     96 
     97 class Date {
     98   int year_, month_, day_;
     99  public:
    100   Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
    101 
    102   int year() const { return year_; }
    103   int month() const { return month_; }
    104   int day() const { return day_; }
    105 };
    106