//------------------------------------------------------------------------------
/*
    This file is part of Beast: https://github.com/vinniefalco/Beast
    Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>

    Permission to use, copy, modify, and/or distribute this software for any
    purpose  with  or without fee is hereby granted, provided that the above
    copyright notice and this permission notice appear in all copies.

    THE  SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH  REGARD  TO  THIS  SOFTWARE  INCLUDING  ALL  IMPLIED  WARRANTIES  OF
    MERCHANTABILITY  AND  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY  SPECIAL ,  DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER  RESULTING  FROM  LOSS  OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION  OF  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

unsigned char const LexicalCastUtilities::s_digitTable [256] = {
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xFF - 0x07
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08 - 0x0F
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10 - 0x17
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18 - 0x1F
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20 - 0x27
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x28 - 0x2F
   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 0x30 - 0x37
   0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38 - 0x3F
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0xC0 - 0xC7
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xC8 - 0xCF
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xD0 - 0xD7
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xD8 - 0xDF
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xE0 - 0xE7
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xE8 - 0xEF
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xF0 - 0xF7
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF  // 0xF8 - 0xFF
};

//------------------------------------------------------------------------------

class LexicalCastTests : public UnitTest
{
public:
    LexicalCastTests () : UnitTest ("LexicalCast", "beast")
    {
    }

    template <class IntType>
    static IntType nextRandomInt (Random& r)
    {
        return static_cast <IntType> (r.nextInt64 ());
    }

    template <class IntType>
    void testInteger (IntType in)
    {
        String s;
        IntType out;

        expect (lexicalCastChecked (s, in));
        expect (lexicalCastChecked (out, s));
        expect (out == in);
    }

    template <class IntType>
    void testIntegers (Random& r)
    {
        {
            String s;
            s << "random " << typeid (IntType).name ();
            beginTestCase (s);

            for (int i = 0; i < 1000; ++i)
            {
                IntType const value (nextRandomInt <IntType> (r));
                testInteger (value);
            }
        }

        {
            String s;
            s << "numeric_limits <" << typeid (IntType).name () << ">";
            beginTestCase (s);

            testInteger (std::numeric_limits <IntType>::min ());
            testInteger (std::numeric_limits <IntType>::max ());
        }
    }

    void runTest ()
    {
        int64 const seedValue = 50;

        Random r (seedValue);

        testIntegers <int> (r);
        testIntegers <unsigned int> (r);
        testIntegers <short> (r);
        testIntegers <unsigned short> (r);
        testIntegers <int32> (r);
        testIntegers <uint32> (r);
        testIntegers <int64> (r);
        testIntegers <uint64> (r);
    }
};

static LexicalCastTests lexicalCastTests;
