Ejemplo n.º 1
0
        public void Basics()
        {
            UT_INIT();

            Log.SetVerbosity( new ConsoleLogger(), Verbosity.Verbose, "/" );
            Log.MapThreadName( "UnitTest" );
            Log.SetDomain( "TickWatch", Scope.Method );

            Log.Info( "\n### TicksBasics ###" );

            // check times
            {
                Ticks t= new Ticks();
                t.FromNanos( 42 );    // beyond resolution in C#:  UT_EQ( t.InNanos(),        42L);
                                        UT_EQ( t.InMicros(),        0L );
                                        UT_EQ( t.InMillis(),        0L );
                                        UT_EQ( t.InSeconds(),        0L );
                t.FromMicros( 42 );    UT_EQ( t.InNanos(),        42000L );
                                        UT_EQ( t.InMicros(),        42L );
                                        UT_EQ( t.InMillis(),        0L );
                                        UT_EQ( t.InSeconds(),        0L );
                t.FromMillis( 42 );    UT_EQ( t.InNanos(),        42000000L );
                                        UT_EQ( t.InMicros(),        42000L );
                                        UT_EQ( t.InMillis(),        42L );
                                        UT_EQ( t.InSeconds(),        0L );
                t.FromSeconds( 42 );    UT_EQ( t.InNanos(),        42000000000L );
                                        UT_EQ( t.InMicros(),        42000000L );
                                        UT_EQ( t.InMillis(),        42000L );
                                        UT_EQ( t.InSeconds(),        42L );
                Ticks diff= new Ticks();
                diff.FromMillis( 100 );
                t.Add( diff );            UT_EQ( t.InNanos(),        42100000000L );
                                        UT_EQ( t.InMicros(),        42100000L );
                                        UT_EQ( t.InMillis(),        42100L );
                                        UT_EQ( t.InSeconds(),        42L );
                t.Sub( diff );            UT_EQ( t.InNanos(),        42000000000L );
                                        UT_EQ( t.InMicros(),        42000000L );
                                        UT_EQ( t.InMillis(),        42000L );
                                        UT_EQ( t.InSeconds(),        42L );

                t.FromMillis( 100 ); UT_EQ( t.InHertz()  ,    10.0 );
                t.FromMillis( 300 ); UT_EQ( t.InHertz(0) ,    3.0 );
                                        UT_EQ( t.InHertz(1) ,    3.3 );
                                        UT_EQ( t.InHertz(2) ,    3.33 );
                                        UT_EQ( t.InHertz(5) ,    3.33333 );
            }

            // check internal frequency
            {
                double freq= Ticks.InternalFrequency;
                Log.Info( "TickWatch InternalFrequency:   " + freq );
                UT_TRUE ( freq >= 1000000.0 );
            }

            // check TickWatch creation time
            {
                Ticks creationTimeDiff=  new Ticks();
                creationTimeDiff.Sub( Ticks.CreationTime );
                Log.Info( "TickWatch library creation was: " + creationTimeDiff.InNanos  () + "ns ago" );
                Log.Info( "TickWatch library creation was: " + creationTimeDiff.InMicros () + "µs ago" );
                Log.Info( "TickWatch library creation was: " + creationTimeDiff.InMillis () + "ms ago" );
                Log.Info( "TickWatch library creation was: " + creationTimeDiff.InSeconds() + "s ago"  );
                UT_TRUE ( creationTimeDiff.InNanos  () > 100  ); // It should really take 100 nanoseconds to get here!
                UT_TRUE ( creationTimeDiff.InSeconds() < 3600 ); // these test will probably not last an hour
            }

            // check if we could sleep for 100ms
            {
                Ticks start= new Ticks();
                ALIB.SleepMillis( 30 );
                Ticks sleepTime= new Ticks();
                sleepTime.Sub( start );
                Log.Info( "TickWatch diff after 100ms sleep: " + sleepTime.InMillis() +" ms" );
                UT_TRUE ( sleepTime.InMillis() > 28 );
                UT_TRUE ( sleepTime.InMillis() < 150 ); // should work even on heavily loaded machines
            }
        }
Ejemplo n.º 2
0
        public void DateTimeConversion()
        {
            UT_INIT();

            Log.SetVerbosity( new ConsoleLogger(), Verbosity.Verbose, "/" );
            Log.MapThreadName( "UnitTest" );
            Log.SetDomain( "TickWatch", Scope.Method );

            Log.Info( "DateTime.MinValue(): "    + ( DateTime.MinValue.ToString() )     );
            Log.Info( "DateTime.MaxValue(): "    + ( DateTime.MaxValue.ToString() )     );

            Ticks ticksNow= new Ticks();

            // roundtrip 1
            long millisEpochNow=    ticksNow.InEpochMillis();
            ticksNow.SetFromEpochMillis( millisEpochNow );
            long ticksNowFromEpoch= ticksNow.Raw();

            UT_TRUE( Math.Abs( ticksNow.Raw() - ticksNowFromEpoch ) < 50000 );

            // roundtrip 2
            long millis5_1_70_3_51=           0
                                      +       (  4L * 24L * 60L * 60L * 1000L )
                                      +       (  3L       * 60L * 60L * 1000L )
                                      +       ( 51L             * 60L * 1000L );


            long millis5_1_71_3_51=            millis5_1_70_3_51
                                      +      ( 365L * 24L * 60L * 60L * 1000L );

            Ticks  ticks5_1_70_3_51= new Ticks(); ticks5_1_70_3_51.SetFromEpochMillis( millis5_1_70_3_51 );
            Ticks  ticks5_1_71_3_51= new Ticks(); ticks5_1_71_3_51.SetFromEpochMillis( millis5_1_71_3_51 );
            UT_TRUE( ticks5_1_70_3_51.Raw() < ticks5_1_71_3_51.Raw() );


            long millis5_1_70_3_51_back= ticks5_1_70_3_51.InEpochMillis();
            long millis5_1_71_3_51_back= ticks5_1_71_3_51.InEpochMillis();


            UT_TRUE( millis5_1_70_3_51_back == millis5_1_70_3_51 );
            UT_TRUE( millis5_1_71_3_51_back == millis5_1_71_3_51 );

            // add 1 day, 2h, 3min and 4sec  days:

            Ticks tomorrow= new Ticks( ticksNow );
            Ticks span= new Ticks();
            span.FromMillis( (long) (new TimeSpan( 1,2,3,4 )).TotalMilliseconds);
            tomorrow.Add ( span );

            Log.Info( "Today: is:        "    +  ( DateTime.Now.ToString()                                 ) );
            Log.Info( "Today: is:        "    +  ( ticksNow                         .InDotNetDateTime().ToString() ) );
            Log.Info( "Today: is:        "    +  ( (new Ticks(ticksNowFromEpoch))   .InDotNetDateTime().ToString() ) );
            Log.Info( "+1d, 2h,3m,4s:    "    +  ( tomorrow                         .InDotNetDateTime().ToString() ) );
            Log.Info( "5.1.70 3:51:00:   "    +  ( ticks5_1_70_3_51                 .InDotNetDateTime().ToString() ) );
            Log.Info( "5.1.71 3:51:00:   "    +  ( ticks5_1_71_3_51                 .InDotNetDateTime().ToString() ) );
        }