Skip to content

Commit

Permalink
Add sun tests and many TODOs
Browse files Browse the repository at this point in the history
This commit adds a rough sketch of tests on the sun, all passing, along
with many TODOs for things I want to refactor.
  • Loading branch information
wapcaplet committed Apr 29, 2020
1 parent 7b3876c commit 3118ab0
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ moon_phase get_moon_phase( const time_point &p )
return static_cast<moon_phase>( current_phase );
}

// TODO: Refactor sunrise / sunset
time_point sunrise( const time_point &p )
{
static_assert( static_cast<int>( SPRING ) == 0,
Expand All @@ -94,6 +95,7 @@ time_point sunrise( const time_point &p )
return midnight + time_duration::from_minutes( static_cast<int>( time * 60 ) );
}

// TODO: Refactor sunrise / sunset
time_point sunset( const time_point &p )
{
static_assert( static_cast<int>( SPRING ) == 0,
Expand Down Expand Up @@ -125,28 +127,35 @@ time_point daylight_time( const time_point &p )
return sunrise( p ) + 15_minutes;
}

// TODO: bool is_day( const time_point &p ) ?

bool is_night( const time_point &p )
{
const time_duration now = time_past_midnight( p );
const time_duration sunrise = time_past_midnight( ::sunrise( p ) );
const time_duration sunset = time_past_midnight( ::sunset( p ) );

// TODO: Use >= sunset + twilight_duration
return now > sunset + twilight_duration || now < sunrise;
}

// TODO: Rename to is_dusk
bool is_sunset_now( const time_point &p )
{
const time_duration now = time_past_midnight( p );
const time_duration sunset = time_past_midnight( ::sunset( p ) );

// TODO: Use >= sunset
return now > sunset && now < sunset + twilight_duration;
}

// TODO: Rename to is_dawn
bool is_sunrise_now( const time_point &p )
{
const time_duration now = time_past_midnight( p );
const time_duration sunrise = time_past_midnight( ::sunrise( p ) );

// TODO: Use >= sunrise
return now > sunrise && now < sunrise + twilight_duration;
}

Expand Down Expand Up @@ -195,15 +204,21 @@ float sunlight( const time_point &p, const bool vision )
0;

if( now > sunset + twilight_duration || now < sunrise ) {
// Night
// if( is_night( p ) )
// Night (after dusk, before sunrise)
return moonlight;
} else if( now >= sunrise && now <= sunrise + twilight_duration ) {
// else if( is_sunrise_now( p ) )
// Dawn (morning twilight)
const double percent = ( now - sunrise ) / twilight_duration;
return static_cast<double>( moonlight ) * ( 1. - percent ) + daylight_level * percent;
} else if( now >= sunset && now <= sunset + twilight_duration ) {
// else if( is_sunset_now( p ) )
// Dusk (evening twilight)
const double percent = ( now - sunset ) / twilight_duration;
return daylight_level * ( 1. - percent ) + static_cast<double>( moonlight ) * percent;
} else {
// Day
return daylight_level;
}
}
Expand Down
144 changes: 144 additions & 0 deletions tests/sun_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include "calendar.h"

#include "catch/catch.hpp"

// SUN TESTS

static const time_point midday = calendar::turn_zero + 12_hours;
static const time_point midnight = calendar::turn_zero + 0_hours;

const time_point today_sunrise = sunrise( midnight );
const time_point today_sunset = sunset( midnight );

// is_night, is_sunrise_now, is_sunset_now
TEST_CASE( "daily solar cycle", "[sun][cycle]" )
{
// INFO( "Sunrise today: " + to_string( today_sunrise ) ); // 6:00 AM
// INFO( "Sunset today: " + to_string( today_sunset ) ); // 7:00 PM

// Night
CHECK( is_night( midnight ) );
CHECK( is_night( midnight + 1_hours ) );
CHECK( is_night( midnight + 2_hours ) );
CHECK( is_night( midnight + 3_hours ) );
CHECK( is_night( midnight + 4_hours ) );
CHECK( is_night( midnight + 5_hours ) );

// FIXME: This time is neither night nor sunrise
CHECK_FALSE( is_night( today_sunrise ) );
CHECK_FALSE( is_sunrise_now( today_sunrise ) );

// Dawn
CHECK( is_sunrise_now( today_sunrise + 1_seconds ) );
CHECK( is_sunrise_now( today_sunrise + 30_minutes ) );
CHECK( is_sunrise_now( today_sunrise + 59_minutes ) );
CHECK_FALSE( is_sunrise_now( today_sunrise + 1_hours ) );

// FIXME: No function to tell when it is daytime
//CHECK( is_day( midday ) );

// FIXME: Sunset does not begin at sunset
CHECK_FALSE( is_sunset_now( today_sunset ) );

// Dusk
CHECK( is_sunset_now( today_sunset + 1_seconds ) );
CHECK( is_sunset_now( today_sunset + 30_minutes ) );
CHECK( is_sunset_now( today_sunset + 59_minutes ) );

// FIXME: This time is neither dusk nor night
CHECK_FALSE( is_sunset_now( today_sunset + 1_hours ) );
CHECK_FALSE( is_night( today_sunset + 1_hours ) );

// Night again
CHECK( is_night( today_sunset + 1_hours + 1_seconds ) );
CHECK( is_night( today_sunset + 2_hours ) );
CHECK( is_night( today_sunset + 3_hours ) );
CHECK( is_night( today_sunset + 4_hours ) );
}

TEST_CASE( "sunlight", "[sun]" )
{
CHECK( sunlight( midnight ) == 1.0f );
CHECK( sunlight( today_sunrise ) == 1.0f );
CHECK( sunlight( today_sunrise + 15_minutes ) == 25.75f );
CHECK( sunlight( today_sunrise + 30_minutes ) == 50.50f );
CHECK( sunlight( today_sunrise + 45_minutes ) == 75.25f );
CHECK( sunlight( midday ) == default_daylight_level() ); // 100.0
CHECK( sunlight( today_sunset ) == default_daylight_level() ); // 100.0
CHECK( sunlight( today_sunset + 15_minutes ) == 75.25f );
CHECK( sunlight( today_sunset + 30_minutes ) == 50.50f );
CHECK( sunlight( today_sunset + 45_minutes ) == 25.75f );
}

TEST_CASE( "sunrise", "[sun][sunrise]" )
{
const time_point spring = calendar::turn_zero;
const time_point summer = spring + 91_days;
const time_point autumn = summer + 91_days;
const time_point winter = autumn + 91_days;

CHECK( "Year 1, Spring, day 1 6:00:00 AM" == to_string( sunrise( spring ) ) );
CHECK( "Year 1, Spring, day 31 5:40:00 AM" == to_string( sunrise( spring + 30_days ) ) );
CHECK( "Year 1, Spring, day 61 5:20:00 AM" == to_string( sunrise( spring + 60_days ) ) );
CHECK( "Year 1, Spring, day 91 5:00:00 AM" == to_string( sunrise( spring + 90_days ) ) );

CHECK( "Year 1, Summer, day 1 5:00:00 AM" == to_string( sunrise( summer ) ) );
CHECK( "Year 1, Summer, day 31 5:19:00 AM" == to_string( sunrise( summer + 30_days ) ) );
CHECK( "Year 1, Summer, day 61 5:39:00 AM" == to_string( sunrise( summer + 60_days ) ) );
CHECK( "Year 1, Summer, day 91 5:59:00 AM" == to_string( sunrise( summer + 90_days ) ) );

CHECK( "Year 1, Autumn, day 1 6:00:00 AM" == to_string( sunrise( autumn ) ) );
CHECK( "Year 1, Autumn, day 31 6:19:00 AM" == to_string( sunrise( autumn + 30_days ) ) );
CHECK( "Year 1, Autumn, day 61 6:39:00 AM" == to_string( sunrise( autumn + 60_days ) ) );
CHECK( "Year 1, Autumn, day 91 6:59:00 AM" == to_string( sunrise( autumn + 90_days ) ) );

CHECK( "Year 1, Winter, day 1 7:00:00 AM" == to_string( sunrise( winter ) ) );
CHECK( "Year 1, Winter, day 31 6:40:00 AM" == to_string( sunrise( winter + 30_days ) ) );
CHECK( "Year 1, Winter, day 61 6:20:00 AM" == to_string( sunrise( winter + 60_days ) ) );
CHECK( "Year 1, Winter, day 91 6:00:00 AM" == to_string( sunrise( winter + 90_days ) ) );
}

TEST_CASE( "sunset", "[sun][sunset]" )
{
const time_point spring = calendar::turn_zero;
const time_point summer = spring + 91_days;
const time_point autumn = summer + 91_days;
const time_point winter = autumn + 91_days;

CHECK( "Year 1, Spring, day 1 7:00:00 PM" == to_string( sunset( spring ) ) );
CHECK( "Year 1, Spring, day 31 7:39:00 PM" == to_string( sunset( spring + 30_days ) ) );
CHECK( "Year 1, Spring, day 61 8:19:00 PM" == to_string( sunset( spring + 60_days ) ) );
CHECK( "Year 1, Spring, day 91 8:58:00 PM" == to_string( sunset( spring + 90_days ) ) );

CHECK( "Year 1, Summer, day 1 9:00:00 PM" == to_string( sunset( summer ) ) );
CHECK( "Year 1, Summer, day 31 8:20:00 PM" == to_string( sunset( summer + 30_days ) ) );
CHECK( "Year 1, Summer, day 61 7:40:00 PM" == to_string( sunset( summer + 60_days ) ) );
CHECK( "Year 1, Summer, day 91 7:01:00 PM" == to_string( sunset( summer + 90_days ) ) );

CHECK( "Year 1, Autumn, day 1 7:00:00 PM" == to_string( sunset( autumn ) ) );
CHECK( "Year 1, Autumn, day 31 6:20:00 PM" == to_string( sunset( autumn + 30_days ) ) );
CHECK( "Year 1, Autumn, day 61 5:40:00 PM" == to_string( sunset( autumn + 60_days ) ) );
CHECK( "Year 1, Autumn, day 91 5:01:00 PM" == to_string( sunset( autumn + 90_days ) ) );

CHECK( "Year 1, Winter, day 1 5:00:00 PM" == to_string( sunset( winter ) ) );
CHECK( "Year 1, Winter, day 31 5:39:00 PM" == to_string( sunset( winter + 30_days ) ) );
CHECK( "Year 1, Winter, day 61 6:19:00 PM" == to_string( sunset( winter + 60_days ) ) );
CHECK( "Year 1, Winter, day 91 6:58:00 PM" == to_string( sunset( winter + 90_days ) ) );
}

TEST_CASE( "current daylight level", "[sun][daylight]" )
{
// TODO
// based on season_of_year
// SPRING
// SUMMER
// AUTUMN
// WINTER
}

TEST_CASE( "full moon", "[moon]" )
{
const time_point full_moon = calendar::turn_zero + calendar::season_length() / 6;
REQUIRE( get_moon_phase( full_moon ) == MOON_FULL );
}

0 comments on commit 3118ab0

Please sign in to comment.