/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*-
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 * Editor Settings: expandtabs and use 4 spaces for indentation */

/*
 * Copyright Likewise Software
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the license, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.  You should have received a copy
 * of the GNU Lesser General Public License along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * LESSER GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Authors: David Leimbach <dleimbach@likewise.com>
 */

#include <moonunit/moonunit.h>
#include "lw/rtlstring.h"

MU_TEST(RtlAnsi, ansi_simple)
{
    ANSI_STRING string = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, "42"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocatePrintf(&string, "%d", 42), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    RtlAnsiStringFree(&string);
    RtlAnsiStringFree(&expected);
}

MU_TEST(RtlAnsi, ansi_null)
{
    ANSI_STRING string = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, "(null)"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocatePrintf(&string, "%s", (char *)0), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    RtlAnsiStringFree(&string);
    RtlAnsiStringFree(&expected);
}

MU_TEST(RtlAnsi, ansi_nil)
{
    ANSI_STRING string = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, sizeof(size_t) == 4 ? "00000000" : "0000000000000000"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocatePrintf(&string, "%p", (void *)0), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    LwRtlAnsiStringFree(&string);
    RtlAnsiStringFree(&expected);
}

MU_TEST(RtlAnsi, ansi_ansi)
{
    ANSI_STRING string = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, "Hello World"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocatePrintf(&string, "%Z", &expected), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    RtlAnsiStringFree(&string);
    RtlAnsiStringFree(&expected);
}

MU_TEST(RtlAnsi, ansi_append)
{
    ANSI_STRING string = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, "Hello World"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocatePrintf(&string, "%s", "Hello"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateAppendPrintf(&string, " %s", "World"), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    RtlAnsiStringFree(&string);
    RtlAnsiStringFree(&expected);
}

MU_TEST(RtlAnsi, ansi_unicode)
{
    ANSI_STRING string = {0};
    UNICODE_STRING message = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, "Hello World"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlUnicodeStringAllocateFromCString(&message, "Hello World"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocatePrintf(&string, "%wZ", &message), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    RtlAnsiStringFree(&string);
    RtlAnsiStringFree(&expected);
    RtlUnicodeStringFree(&message);
}

MU_TEST(RtlAnsi, ansi_append_unicode)
{
    ANSI_STRING string = {0};
    UNICODE_STRING string2 = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, "Hello World"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlUnicodeStringAllocateFromCString(&string2, "World"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocatePrintf(&string, "%s", "Hello"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateAppendPrintf(&string, " %wZ", &string2), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    RtlAnsiStringFree(&string);
    RtlUnicodeStringFree(&string2);
    RtlAnsiStringFree(&expected);
}

MU_TEST(RtlAnsi, ansi_append_to_null)
{
    ANSI_STRING string = {0};
    ANSI_STRING expected = {0};

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateFromCString(&expected, "Hello World"), 0);

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlAnsiStringAllocateAppendPrintf(&string, "%s%s", "Hello"," World"), 0);

    MU_ASSERT(RtlAnsiStringIsEqual(&expected, &string, 1));

    RtlAnsiStringFree(&string);
    RtlAnsiStringFree(&expected);
}
