/* -*- 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(RtlUnicode, unicode_simple)
{
    UNICODE_STRING string = {0};
    UNICODE_STRING expected = {0};

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

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

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

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

MU_TEST(RtlUnicode, unicode_null)
{
    UNICODE_STRING string = {0};
    UNICODE_STRING expected = {0};

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

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

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

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

MU_TEST(RtlUnicode, unicode_nil)
{
    UNICODE_STRING string = {0};
    UNICODE_STRING expected = {0};

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

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

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

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

MU_TEST(RtlUnicode, unicode_unicode)
{
    UNICODE_STRING string = {0};
    UNICODE_STRING expected = {0};

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

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlUnicodeStringAllocatePrintf(&string, "%wZ", &expected), 0);

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

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

MU_TEST(RtlUnicode, unicode_ansi)
{
    UNICODE_STRING string = {0};
    ANSI_STRING message = {0};
    UNICODE_STRING expected = {0};

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

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

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlUnicodeStringAllocatePrintf(&string, "%Z", &message), 0);

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

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

MU_TEST(RtlUnicode, unicode_append)
{
    UNICODE_STRING string = {0};
    UNICODE_STRING expected = {0};

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

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

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

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

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

MU_TEST(RtlUnicode, unicode_append_ansi)
{
    UNICODE_STRING string = {0};
    ANSI_STRING string2 = {0};
    UNICODE_STRING expected = {0};

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

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

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

    MU_ASSERT_EQUAL(MU_TYPE_INTEGER, RtlUnicodeStringAllocateAppendPrintf(&string, " %Z", &string2), 0);

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

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

MU_TEST(RtlUnicode, unicode_append_to_null)
{
    UNICODE_STRING string = {0};
    UNICODE_STRING expected = {0};

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

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

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

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

