⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdate.java

📁 java base64
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        // pause after every three items
        count++;
        if ( count >= 3 )
            {
            // will reset to 0
            pause();
            }
        else
            {
            System.out.println();
            }
        }

    /**
     * Display a date and its ordinal on System.out.
     *
     * @param desc a description of the date to label it.
     * @param yyyy the year to display.
     * @param mm   the month to display.
     * @param dd   the day of the month to display.
     */
    private static void show( String desc, int yyyy, int mm, int dd )
        {
        System.out.println( desc + BigDate.toOrdinal( yyyy, mm, dd ) );
        }

    /**
     * Test that the age routine generates plausible results.
     *
     * @param fromDate    earliest date to test.
     * @param toDate      last date to test.
     * @param samples     how many random pairs to test.
     * @param showDetails true if want details of individual tests
     */
    private static void testAge( BigDate fromDate,
                                 BigDate toDate,
                                 int samples,
                                 boolean showDetails )
        {
        // test with a random sampling of date pairs.

        System.out
                .println( "Testing age from: "
                          + fromDate.toString()
                          + " to: "
                          + toDate.toString()
                          + " using "
                          + samples
                          + " random samples." );

        Random wheel = new Random();
        BigDate birth = new BigDate();
        BigDate asof = new BigDate();
        // date after which our approximations hold.
        int plausibleYear = BigDate.isBritish ? 1752 : 1582;

        // calculate transform to take result of Random.nextInt into our range.
        int base = fromDate.getOrdinal();
        int divisor = toDate.getOrdinal() - base + 1;
        if ( divisor < 2 )
            {
            divisor = 2;
            }

        // Any difference 4 or less is reasonable because of the way months have
        // unequal lengths. Only flag really bad cases. There should not be any.
        int worstApprox = 4;
        for ( int i = 0; i < samples; i++ )
            {
            // Generate a pair of random dates in range. Might not be in order.
            int ord1 = ( wheel.nextInt() & Integer.MAX_VALUE ) % divisor + base;
            int ord2 = ( wheel.nextInt() & Integer.MAX_VALUE ) % divisor + base;
            // put them in order
            if ( ord1 > ord2 )
                {
                int temp = ord1;
                ord1 = ord2;
                ord2 = temp;
                }
            birth.set( ord1 );
            asof.set( ord2 );

            int[] age = BigDate.age( birth, asof );

            if ( showDetails )
                {
                System.out.print( "birth: " + birth.toString() + " " );
                System.out.print( "as of: " + asof.toString() + " " );
                System.out
                        .println( age[ 0 ]
                                  + " years and "
                                  + age[ 1 ]
                                  + " months and "
                                  + age[ 2 ]
                                  + " days old." );
                }

            if ( age[ 0 ] < 0 )
                {
                fail( "Negative age in years" );
                }
            if ( age[ 1 ] < 0 )
                {
                fail( "Negative age in months" );
                }
            if ( age[ 2 ] < 0 )
                {
                fail( "Negative age in days" );
                }

            if ( age[ 1 ] > 11 )
                {
                fail( "Age > 11 months" );
                }
            if ( age[ 2 ] > 31 )
                {
                fail( "Age > 31 days" );
                }
            if ( age[ 0 ] > 0
                 && age[ 1 ] > 0
                 && age[ 2 ] > 0
                 && birth.getYYYY() > plausibleYear )
                {
                // plausibility test, approximation works after Gregorian
                // instituted.
                int roughAgeInDays =
                        ( int ) Math.round( age[ 0 ] * 365.2425d
                                            + age[ 1 ] * 30.436875d
                                            + age[ 2 ] );
                int exactAgeInDays = asof.getOrdinal() - birth.getOrdinal();
                int approx = Math.abs( roughAgeInDays - exactAgeInDays );
                if ( approx > worstApprox )
                    {
                    worstApprox = approx;
                    System.out.print( "birth: " + birth.toString() + " " );
                    System.out.print( "as of: " + asof.toString() + " " );
                    System.out
                            .println( age[ 0 ]
                                      + " years and "
                                      + age[ 1 ]
                                      + " months and "
                                      + age[ 2 ]
                                      + " days old. Differs from approx by "
                                      + approx
                                      + "." );
                    }// end if got a new worst approximation
                }// end if plausibility test
            }// end for
        }// end testAge

    /**
     * Check for bugs in toOrdinal and toGregorian.
     *
     * @param fromYear the first year to test.
     * @param toYear   the last year to test.
     */
    private static void testDate( int fromYear, int toYear )
        {
        int gotOrdinal, expectedOrdinal;
        BigDate gotGregorian;
        int gotYear, expectedYear;
        int gotMonth, expectedMonth;
        int gotDay, expectedDay;

        System.out
                .println( "Testing toOrdinal and toGregorian "
                          + fromYear
                          + " .. "
                          + toYear );

        System.out.println( "This could take a while..." );
        try
            {
            expectedOrdinal = BigDate.toOrdinal( fromYear, 1, 1 );

            for ( expectedYear = fromYear; expectedYear
                                           <= toYear; expectedYear++ )
                {
                if ( expectedYear % 10000 == 0 )
                    {
                    System.out.println( "reached " + expectedYear );
                    }

                for ( expectedMonth = 1; expectedMonth <= 12; expectedMonth++ )
                    {
                    for ( expectedDay = 1; expectedDay <= 31; expectedDay++ )
                        {
                        if ( BigDate.isValid( expectedYear,
                                expectedMonth,
                                expectedDay ) )
                            {
                            // test toOrdinal
                            gotOrdinal =
                                    BigDate.toOrdinal( expectedYear,
                                            expectedMonth,
                                            expectedDay );
                            if ( gotOrdinal != expectedOrdinal )
                                {
                                fail( "toOrdinal oops "
                                      + " expected: "
                                      + " YYYY:"
                                      + expectedYear
                                      + " MM:"
                                      + expectedMonth
                                      + " DD:"
                                      + expectedDay
                                      + " JJJJ:"
                                      + expectedOrdinal
                                      + " got: "
                                      + " JJJJ:"
                                      + gotOrdinal );
                                }// end if

                            // test toGregorian
                            gotGregorian = new BigDate( expectedOrdinal );
                            gotYear = gotGregorian.getYYYY();
                            gotMonth = gotGregorian.getMM();
                            gotDay = gotGregorian.getDD();

                            if ( ( gotYear != expectedYear )
                                 || ( gotMonth != expectedMonth )
                                 || ( gotDay != expectedDay ) )
                                {
                                fail( "toGregorian failed"
                                      + " expected: "
                                      + " JJJJ:"
                                      + expectedOrdinal
                                      + " YYYY:"
                                      + expectedYear
                                      + " MM:"
                                      + expectedMonth
                                      + " DD:"
                                      + expectedDay
                                      + " got: "
                                      + " YYYY:"
                                      + gotYear
                                      + " MM:"
                                      + gotMonth
                                      + " DD:"
                                      + gotDay );
                                }// end if

                            // increment only for valid dates
                            expectedOrdinal = gotOrdinal + 1;
                            }// end if isValid
                        }
                    }
                }// all three for loops
            }// end try
        catch ( IllegalArgumentException e )
            {
            fail( "test failed " + e.getMessage() );
            }
        System.out
                .println(
                        "BigDate toOrdinal and toGregorian test completed successfully" );
        }// end testDate

    /**
     * Test getISOWeekNumber and getISODayOfWeek.
     *
     * @param fromYear first year to test.
     * @param toYear   last year to test.
     * @noinspection SameParameterValue
     */
    private static void testISOWeekNumber( int fromYear, int toYear )
        {
        BigDate start = new BigDate( fromYear, 1, 1 );
        BigDate stop = new BigDate( toYear, 12, 31 );
        System.out
                .println( "Testing getISOWeekNumber and getISODayOfWeek "
                          + start.toString()
                          + " .. "
                          + stop.toString() );

        BigDate candidate = new BigDate( start );
        int expectedWeekNumber = candidate.getISOWeekNumber();
        int expectedDayOfWeek = candidate.getISODayOfWeek();

        for ( int i = start.getOrdinal(); i < stop.getOrdinal(); i++ )
            {
            candidate.setOrdinal( i );
            int weekNumber = candidate.getISOWeekNumber();
            int dayOfWeek = candidate.getISODayOfWeek();
            if ( weekNumber != expectedWeekNumber )
                {
                if ( expectedWeekNumber >= 53 && weekNumber == 1 )
                    {
                    expectedWeekNumber = 1;
                    }
                else
                    {
                    fail( "getISOWeekNumber bug at " + candidate.toString() );
                    }
                }
            if ( dayOfWeek != expectedDayOfWeek )
                {
                fail( "getISoDayOfWeek bug at " + candidate.toString() );
                }
            expectedDayOfWeek = dayOfWeek + 1;
            if ( expectedDayOfWeek == 8 )
                {
                expectedDayOfWeek = 1;
                expectedWeekNumber++;
                }
            }
        }// end testIOSWeekNumber

    /**
     * make sure testNearestXXX Day is working correctly
     */
    private static void testNearestXXXDay()
        {
        System.out.println( "Testing nearestXXXDay" );
        BigDate start = new BigDate( 2007, 10, 14 );
        BigDate stop = new BigDate( 2007, 10, 21 );
        for ( int i = start.getOrdinal(); i < stop.getOrdinal(); i++ )
            {
            BigDate aDay = new BigDate( i );
            for ( int day = 0; day < 7; day++ )
                {
                BigDate nearest = aDay.nearestXXXDay( day );

                if ( nearest.getDayOfWeek() != day )
                    {
                    System.err
                            .println( "wrong day of week for nearstXXXDay base:"
                                      + aDay
                                      + " nearest:"
                                      + nearest
                                      + " day:"
                                      + shortDayName[ day ] );
                    }
                if ( Math.abs( nearest.getOrdinal() - i ) >= 4 )
                    {
                    System.err
            

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -