Merge branch 'test'.

The unit tests are still very much a work in progress.
But the work now lives on the master branch.
This commit is contained in:
Howard Hinnant 2015-08-08 12:51:54 -04:00
commit 0fe43bab32
10 changed files with 605 additions and 0 deletions

125
test/date_test/day.pass.cpp Normal file
View File

@ -0,0 +1,125 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// class day
// {
// unsigned char d_;
// public:
// explicit constexpr day(unsigned d) noexcept;
//
// day& operator++() noexcept;
// day operator++(int) noexcept;
// day& operator--() noexcept;
// day operator--(int) noexcept;
//
// day& operator+=(const days& d) noexcept;
// day& operator-=(const days& d) noexcept;
//
// constexpr explicit operator unsigned() const noexcept;
// constexpr bool ok() const noexcept;
// };
//
// constexpr bool operator==(const day& x, const day& y) noexcept;
// constexpr bool operator!=(const day& x, const day& y) noexcept;
// constexpr bool operator< (const day& x, const day& y) noexcept;
// constexpr bool operator> (const day& x, const day& y) noexcept;
// constexpr bool operator<=(const day& x, const day& y) noexcept;
// constexpr bool operator>=(const day& x, const day& y) noexcept;
//
// constexpr day operator+(const day& x, const days& y) noexcept;
// constexpr day operator+(const days& x, const day& y) noexcept;
// constexpr day operator-(const day& x, const days& y) noexcept;
// constexpr days operator-(const day& x, const day& y) noexcept;
//
// constexpr day operator "" _d(unsigned long long d) noexcept;
// std::ostream& operator<<(std::ostream& os, const day& d);
#include "date.h"
#include <cassert>
#include <sstream>
#include <type_traits>
static_assert( std::is_trivially_destructible<date::day>{}, "");
static_assert(!std::is_default_constructible<date::day>{}, "");
static_assert( std::is_trivially_copy_constructible<date::day>{}, "");
static_assert( std::is_trivially_copy_assignable<date::day>{}, "");
static_assert( std::is_trivially_move_constructible<date::day>{}, "");
static_assert( std::is_trivially_move_assignable<date::day>{}, "");
static_assert( std::is_nothrow_constructible<date::day, unsigned>{}, "");
static_assert( std::is_nothrow_constructible<unsigned, date::day>{}, "");
static_assert(!std::is_convertible<unsigned, date::day>{}, "");
static_assert(!std::is_convertible<date::day, unsigned>{}, "");
static_assert(static_cast<unsigned>(date::day{1}) == 1, "");
static_assert(!date::day{0}.ok(), "");
static_assert( date::day{1}.ok(), "");
static_assert( date::day{2}.ok(), "");
static_assert( date::day{3}.ok(), "");
static_assert( date::day{29}.ok(), "");
static_assert( date::day{30}.ok(), "");
static_assert( date::day{31}.ok(), "");
static_assert(!date::day{32}.ok(), "");
int
main()
{
using namespace date;
static_assert(std::is_same<decltype(1_d), date::day>{}, "");
static_assert(1_d == date::day{1}, "");
static_assert(2_d == date::day{2}, "");
static_assert( 1_d == 1_d, "");
static_assert(!(1_d == 2_d), "");
static_assert(!(2_d == 1_d), "");
static_assert(!(1_d != 1_d), "");
static_assert( 1_d != 2_d, "");
static_assert( 2_d != 1_d, "");
static_assert(!(1_d < 1_d), "");
static_assert( 1_d < 2_d, "");
static_assert(!(2_d < 1_d), "");
static_assert( 1_d <= 1_d, "");
static_assert( 1_d <= 2_d, "");
static_assert(!(2_d <= 1_d), "");
static_assert(!(1_d > 1_d), "");
static_assert(!(1_d > 2_d), "");
static_assert( 2_d > 1_d, "");
static_assert( 1_d >= 1_d, "");
static_assert(!(1_d >= 2_d), "");
static_assert( 2_d >= 1_d, "");
static_assert(3_d + days{7} == 10_d, "");
static_assert(days{7} + 3_d == 10_d, "");
static_assert(3_d + weeks{1} == 10_d, "");
static_assert(weeks{1} + 3_d == 10_d, "");
static_assert(7_d - days{3} == 4_d, "");
static_assert(3_d - 7_d == days{-4}, "");
static_assert(25_d - 11_d == weeks{2}, "");
auto d = 1_d;
assert(++d == 2_d);
assert(d++ == 2_d);
assert(d == 3_d);
assert(d-- == 3_d);
assert(d == 2_d);
assert(--d == 1_d);
assert((d += days{2}) == 3_d);
assert((d -= days{2}) == 1_d);
std::ostringstream os;
os << d;
assert(os.str() == "01");
d += days{11};
os.str("");
os << d;
assert(os.str() == "12");
}

View File

@ -0,0 +1,14 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// day + day not allowed
#include "date.h"
int
main()
{
using namespace date;
auto x = 3_d + 7_d;
}

View File

@ -0,0 +1,14 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// days - day not allowed
#include "date.h"
int
main()
{
using namespace date;
auto x = days{3} - 7_d;
}

View File

@ -0,0 +1,77 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// durations
//
// using days = std::chrono::duration
// <int, std::ratio_multiply<std::ratio<24>, std::chrono::hours::period>>;
//
// using weeks = std::chrono::duration
// <int, std::ratio_multiply<std::ratio<7>, days::period>>;
//
// using years = std::chrono::duration
// <int, std::ratio_multiply<std::ratio<146097, 400>, days::period>>;
//
// using months = std::chrono::duration
// <int, std::ratio_divide<years::period, std::ratio<12>>>;
//
// time_point
//
// using day_point = std::chrono::time_point<std::chrono::system_clock, days>;
#include "date.h"
#include <type_traits>
static_assert(date::days{1} == std::chrono::seconds{86400}, "");
static_assert(date::days{1} == std::chrono::hours{24}, "");
static_assert( std::is_convertible<date::days, std::chrono::seconds>{}, "");
static_assert( std::is_convertible<date::days, std::chrono::minutes>{}, "");
static_assert( std::is_convertible<date::days, std::chrono::hours>{}, "");
static_assert(!std::is_convertible<date::days, date::weeks>{}, "");
static_assert(!std::is_convertible<date::days, date::months>{}, "");
static_assert(!std::is_convertible<date::days, date::years>{}, "");
static_assert(date::weeks{1} == std::chrono::seconds{604800}, "");
static_assert(date::weeks{1} == date::days{7}, "");
static_assert( std::is_convertible<date::weeks, std::chrono::seconds>{}, "");
static_assert( std::is_convertible<date::weeks, std::chrono::minutes>{}, "");
static_assert( std::is_convertible<date::weeks, std::chrono::hours>{}, "");
static_assert( std::is_convertible<date::weeks, date::days>{}, "");
static_assert(!std::is_convertible<date::weeks, date::months>{}, "");
static_assert(!std::is_convertible<date::weeks, date::years>{}, "");
static_assert(date::months{1} == std::chrono::seconds{2629746}, "");
static_assert(date::days{30} < date::months{1} && date::months{1} < date::days{31}, "");
static_assert(date::weeks{4} < date::months{1} && date::months{1} < date::weeks{5}, "");
static_assert(!std::is_convertible<date::months, date::years>{}, "");
static_assert(!std::is_convertible<date::months, date::weeks>{}, "");
static_assert(!std::is_convertible<date::months, date::days>{}, "");
static_assert(!std::is_convertible<date::months, std::chrono::hours>{}, "");
static_assert(!std::is_convertible<date::months, std::chrono::minutes>{}, "");
static_assert( std::is_convertible<date::months, std::chrono::seconds>{}, "");
static_assert( std::is_convertible<date::months, std::chrono::milliseconds>{}, "");
static_assert( std::is_convertible<date::months, std::chrono::microseconds>{}, "");
static_assert( std::is_convertible<date::months, std::chrono::nanoseconds>{}, "");
static_assert(date::years{1} == std::chrono::seconds{31556952}, "");
static_assert(date::days{365} < date::years{1} && date::years{1} < date::days{366}, "");
static_assert(date::weeks{52} < date::years{1} && date::years{1} < date::weeks{53}, "");
static_assert(date::years{1} == date::months{12}, "");
static_assert( std::is_convertible<date::years, date::months>{}, "");
static_assert(!std::is_convertible<date::years, date::weeks>{}, "");
static_assert(!std::is_convertible<date::years, date::days>{}, "");
static_assert(!std::is_convertible<date::years, std::chrono::hours>{}, "");
static_assert(!std::is_convertible<date::years, std::chrono::minutes>{}, "");
static_assert( std::is_convertible<date::years, std::chrono::seconds>{}, "");
static_assert( std::is_convertible<date::years, std::chrono::milliseconds>{}, "");
static_assert( std::is_convertible<date::years, std::chrono::microseconds>{}, "");
static_assert( std::is_convertible<date::years, std::chrono::nanoseconds>{}, "");
static_assert(std::is_same<date::day_point::duration, date::days>{}, "");
int
main()
{
}

View File

@ -0,0 +1,16 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// constexpr struct last_spec {} last{};
#include "date.h"
#include <type_traits>
static_assert(std::is_same<decltype(date::last), const date::last_spec>{}, "");
int
main()
{
}

View File

@ -0,0 +1,163 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// class month
// {
// unsigned char m_;
// public:
// explicit constexpr month(unsigned m) noexcept;
//
// month& operator++() noexcept;
// month operator++(int) noexcept;
// month& operator--() noexcept;
// month operator--(int) noexcept;
//
// month& operator+=(const months& m) noexcept;
// month& operator-=(const months& m) noexcept;
//
// constexpr explicit operator unsigned() const noexcept;
// constexpr bool ok() const noexcept;
// };
//
// constexpr bool operator==(const month& x, const month& y) noexcept;
// constexpr bool operator!=(const month& x, const month& y) noexcept;
// constexpr bool operator< (const month& x, const month& y) noexcept;
// constexpr bool operator> (const month& x, const month& y) noexcept;
// constexpr bool operator<=(const month& x, const month& y) noexcept;
// constexpr bool operator>=(const month& x, const month& y) noexcept;
//
// constexpr month operator+(const month& x, const months& y) noexcept;
// constexpr month operator+(const months& x, const month& y) noexcept;
// constexpr month operator-(const month& x, const months& y) noexcept;
// constexpr months operator-(const month& x, const month& y) noexcept;
//
// std::ostream& operator<<(std::ostream& os, const month& m);
// constexpr month jan{1};
// constexpr month feb{2};
// constexpr month mar{3};
// constexpr month apr{4};
// constexpr month may{5};
// constexpr month jun{6};
// constexpr month jul{7};
// constexpr month aug{8};
// constexpr month sep{9};
// constexpr month oct{10};
// constexpr month nov{11};
// constexpr month dec{12};
#include "date.h"
#include <cassert>
#include <sstream>
#include <type_traits>
static_assert( std::is_trivially_destructible<date::month>{}, "");
static_assert(!std::is_default_constructible<date::month>{}, "");
static_assert( std::is_trivially_copy_constructible<date::month>{}, "");
static_assert( std::is_trivially_copy_assignable<date::month>{}, "");
static_assert( std::is_trivially_move_constructible<date::month>{}, "");
static_assert( std::is_trivially_move_assignable<date::month>{}, "");
static_assert( std::is_nothrow_constructible<date::month, unsigned>{}, "");
static_assert( std::is_nothrow_constructible<unsigned, date::month>{}, "");
static_assert(!std::is_convertible<unsigned, date::month>{}, "");
static_assert(!std::is_convertible<date::month, unsigned>{}, "");
static_assert(static_cast<unsigned>(date::month{1}) == 1, "");
static_assert(!date::month{0}.ok(), "");
static_assert( date::month{1}.ok(), "");
static_assert( date::month{2}.ok(), "");
static_assert( date::month{3}.ok(), "");
static_assert( date::month{4}.ok(), "");
static_assert( date::month{5}.ok(), "");
static_assert( date::month{6}.ok(), "");
static_assert( date::month{7}.ok(), "");
static_assert( date::month{8}.ok(), "");
static_assert( date::month{9}.ok(), "");
static_assert( date::month{10}.ok(), "");
static_assert( date::month{11}.ok(), "");
static_assert( date::month{12}.ok(), "");
static_assert(!date::month{13}.ok(), "");
int
main()
{
using namespace date;
static_assert(jan == date::month{1}, "");
static_assert(feb == date::month{2}, "");
static_assert(mar == date::month{3}, "");
static_assert(apr == date::month{4}, "");
static_assert(may == date::month{5}, "");
static_assert(jun == date::month{6}, "");
static_assert(jul == date::month{7}, "");
static_assert(aug == date::month{8}, "");
static_assert(sep == date::month{9}, "");
static_assert(oct == date::month{10}, "");
static_assert(nov == date::month{11}, "");
static_assert(dec == date::month{12}, "");
static_assert(!(jan != jan), "");
static_assert( jan != feb, "");
static_assert( feb != jan, "");
static_assert(!(jan < jan), "");
static_assert( jan < feb, "");
static_assert(!(feb < jan), "");
static_assert( jan <= jan, "");
static_assert( jan <= feb, "");
static_assert(!(feb <= jan), "");
static_assert(!(jan > jan), "");
static_assert(!(jan > feb), "");
static_assert( feb > jan, "");
static_assert( jan >= jan, "");
static_assert(!(jan >= feb), "");
static_assert( feb >= jan, "");
assert(mar + months{7} == oct);
assert(mar + months{27} == jun);
assert(months{7} + mar == oct);
assert(months{27} + mar == jun);
assert(mar - months{7} == aug);
assert(mar - months{27} == dec);
assert(mar - feb == months{1});
assert(feb - mar == months{11});
#if __cplusplus >= 201402
static_assert(mar + months{7} == oct, "");
static_assert(mar + months{27} == jun, "");
static_assert(months{7} + mar == oct, "");
static_assert(months{27} + mar == jun, "");
static_assert(mar - months{7} == aug, "");
static_assert(mar - months{27} == dec, "");
static_assert(mar - feb == months{1}, "");
static_assert(feb - mar == months{11}, "");
#endif
auto m = dec;
assert(++m == jan);
assert(m++ == jan);
assert(m == feb);
assert(m-- == feb);
assert(m == jan);
assert(--m == dec);
assert((m += months{2}) == feb);
assert((m -= months{2}) == dec);
std::ostringstream os;
os << m;
assert(os.str() == "Dec");
m += months{11};
os.str("");
os << m;
assert(os.str() == "Nov");
}

View File

@ -0,0 +1,14 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// month + month not allowed
#include "date.h"
int
main()
{
using namespace date;
auto x = mar + jul;
}

View File

@ -0,0 +1,14 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
// months - month not allowed
#include "date.h"
int
main()
{
using namespace date;
auto x = months{3} - jul;
}

8
test/just.pass.cpp Normal file
View File

@ -0,0 +1,8 @@
// Howard Hinnant
// This work is licensed under a Creative Commons Attribution 4.0 International License.
// http://creativecommons.org/licenses/by/4.0/
int
main()
{
}

160
test/testit Executable file
View File

@ -0,0 +1,160 @@
#!/bin/sh
# Howard Hinnant
# This work is licensed under a Creative Commons Attribution 4.0 International License.
# http://creativecommons.org/licenses/by/4.0/
currentpath=`pwd`
origpath=$currentpath
currentdir=`basename $currentpath`
while [ $currentdir != "test" ]; do
if [ $currentdir = "/" ]
then
echo "current directory must be in or under \"test\"."
exit 1
fi
cd ..
currentpath=`pwd`
currentdir=`basename $currentpath`
done
cd ..
ROOT=`pwd`
cd $origpath
if [ -z "$CXX" ]
then
CXX=clang++
fi
if [ -z "$CXX_LANG" ]
then
CXX_LANG=c++14
fi
if [ -z "$OPTIONS" ]
then
OPTIONS="-std=${CXX_LANG}"
fi
OPTIONS="$OPTIONS -I$ROOT"
case $TRIPLE in
*-*-mingw* | *-*-cygwin* | *-*-win*)
TEST_EXE=test.exe
;;
*)
TEST_EXE=a.out
;;
esac
case $(uname -s) in
NetBSD)
THREAD_FLAGS=-lpthread
;;
esac
FAIL=0
PASS=0
UNIMPLEMENTED=0
IMPLEMENTED_FAIL=0
IMPLEMENTED_PASS=0
afunc() {
fail=0
pass=0
if (ls ${TEST_PREFIX}*fail.cpp > /dev/null 2>&1)
then
for FILE in $(ls ${TEST_PREFIX}*fail.cpp); do
if $CXX $OPTIONS $HEADER_INCLUDE $SOURCE_LIB $FILE $LIBS -o ./$TEST_EXE > /dev/null 2>&1
then
rm ./$TEST_EXE
echo "$FILE should not compile"
fail=$(($fail+1))
else
pass=$(($pass+1))
fi
done
fi
if (ls ${TEST_PREFIX}*pass.cpp > /dev/null 2>&1)
then
for FILE in $(ls ${TEST_PREFIX}*pass.cpp); do
if [ "$VERBOSE" ]
then
echo "Running test: " $FILE
fi
if $CXX $OPTIONS $HEADER_INCLUDE $SOURCE_LIB $FILE $LIBS $(test $1 = no || echo $THREAD_FLAGS) -o ./$TEST_EXE
then
if ./$TEST_EXE
then
rm ./$TEST_EXE
pass=$(($pass+1))
else
echo "`pwd`/$FILE failed at run time"
echo "Compile line was:" $CXX $OPTIONS $HEADER_INCLUDE $SOURCE_LIB $FILE $LIBS $(test $1 = no || echo $THREAD_FLAGS)
fail=$(($fail+1))
rm ./$TEST_EXE
fi
else
echo "`pwd`/$FILE failed to compile"
echo "Compile line was:" $CXX $OPTIONS $HEADER_INCLUDE $SOURCE_LIB $FILE $LIBS $(test $1 = no || echo $THREAD_FLAGS)
fail=$(($fail+1))
fi
done
fi
if [ $fail -gt 0 ]
then
echo "failed $fail tests in `pwd`"
IMPLEMENTED_FAIL=$(($IMPLEMENTED_FAIL+1))
fi
if [ $pass -gt 0 ]
then
echo "passed $pass tests in `pwd`"
if [ $fail -eq 0 ]
then
IMPLEMENTED_PASS=$((IMPLEMENTED_PASS+1))
fi
fi
if [ $fail -eq 0 -a $pass -eq 0 ]
then
echo "not implemented: `pwd`"
UNIMPLEMENTED=$(($UNIMPLEMENTED+1))
fi
FAIL=$(($FAIL+$fail))
PASS=$(($PASS+$pass))
for FILE in *
do
if [ -d "$FILE" ];
then
cd $FILE
if [ $FILE = thread -o $1 = yes ]; then
afunc yes
else
afunc no
fi
cd ..
fi
done
}
afunc no
echo "****************************************************"
echo "Results for `pwd`:"
echo "using `$CXX --version`"
echo "with $OPTIONS $HEADER_INCLUDE $SOURCE_LIB"
echo "----------------------------------------------------"
echo "sections without tests : $UNIMPLEMENTED"
echo "sections with failures : $IMPLEMENTED_FAIL"
echo "sections without failures: $IMPLEMENTED_PASS"
echo " + ----"
echo "total number of sections : $(($UNIMPLEMENTED+$IMPLEMENTED_FAIL+$IMPLEMENTED_PASS))"
echo "----------------------------------------------------"
echo "number of tests failed : $FAIL"
echo "number of tests passed : $PASS"
echo " + ----"
echo "total number of tests : $(($FAIL+$PASS))"
echo "****************************************************"
exit $FAIL