<commit_before>/*
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * 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 library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

#include "internal.h"
#include "testutils.h"
#include "viralloc.h"
#include "../tools/vsh-table.h"

static int
testVshTableNew(const void *opaque ATTRIBUTE_UNUSED)
{
    if (vshTableNew(NULL)) {
        fprintf(stderr, "expected failure when passing null to vshTableNew\n");
        return -1;
    }

    return 0;
}

static int
testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED)
{
    int ret = 0;
    char *act = NULL;
    const char *exp =
" 1   fedora28   running  \n"
" 2   rhel7.5    running  \n";
    const char *exp2 =
" Id   Name       State    \n"
"--------------------------\n"
" 1    fedora28   running  \n"
" 2    rhel7.5    running  \n";

    vshTablePtr table = vshTableNew("Id", "Name", "State",
                                    NULL); //to ask about return
    if (!table)
        goto cleanup;

    vshTableRowAppend(table, "1", "fedora28", "running", NULL);
    vshTableRowAppend(table, "2", "rhel7.5", "running",
                      NULL);

    act = vshTablePrintToString(table, false);
    if (virTestCompareToString(exp, act) < 0)
        ret = -1;

    VIR_FREE(act);
    act = vshTablePrintToString(table, true);
    if (virTestCompareToString(exp2, act) < 0)
        ret = -1;

 cleanup:
    VIR_FREE(act);
    vshTableFree(table);
    return ret;
}

static int
testVshTableRowAppend(const void *opaque ATTRIBUTE_UNUSED)
{
    int ret = 0;

    vshTablePtr table = vshTableNew("Id", "Name", NULL);
    if (!table)
        goto cleanup;

    if (vshTableRowAppend(table, NULL) >= 0) {
        fprintf(stderr, "Appending NULL shouldn't work\n");
        ret = -1;
    }

    if (vshTableRowAppend(table, "2", NULL) >= 0) {
        fprintf(stderr, "Appending less items than in header\n");
        ret = -1;
    }

    if (vshTableRowAppend(table, "2", "rhel7.5", "running",
                      NULL) >= 0) {
        fprintf(stderr, "Appending more items than in header\n");
        ret = -1;
    }

    if (vshTableRowAppend(table, "2", "rhel7.5", NULL) < 0) {
        fprintf(stderr, "Appending same number of items as in header"
                        " should not return NULL\n");
        ret = -1;
    }

 cleanup:
    vshTableFree(table);
    return ret;
}

static int
testUnicode(const void *opaque ATTRIBUTE_UNUSED)
{

    int ret = 0;
    char *act = NULL;

    const char *exp =
" Id   名稱                  государство  \n"
"-----------------------------------------\n"
" 1    fedora28              running      \n"
" 2    🙊🙉🙈rhel7.5🙆🙆🙅   running      \n";
    vshTablePtr table;

    table = vshTableNew("Id", "名稱", "государство", NULL);
    if (!table)
        goto cleanup;

    vshTableRowAppend(table, "1", "fedora28", "running", NULL);
    vshTableRowAppend(table, "2", "🙊🙉🙈rhel7.5🙆🙆🙅", "running",
                      NULL);

    act = vshTablePrintToString(table, true);
    if (virTestCompareToString(exp, act) < 0)
        ret = -1;

 cleanup:
    VIR_FREE(act);
    vshTableFree(table);
    return ret;
}

/* Point of this test is to see how table behaves with right to left writing*/
static int
testUnicodeArabic(const void *opaque ATTRIBUTE_UNUSED)
{

    int ret = 0;<commit_msg>vsh-table: Fix broken build on centos and rhel<commit_after>/*
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * 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 library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

#include "internal.h"
#include "testutils.h"
#include "viralloc.h"
#include "../tools/vsh-table.h"

static int
testVshTableNew(const void *opaque ATTRIBUTE_UNUSED)
{
    if (vshTableNew(NULL)) {
        fprintf(stderr, "expected failure when passing null to vshTableNew\n");
        return -1;
    }

    return 0;
}

static int
testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED)
{
    int ret = 0;
    char *act = NULL;
    const char *exp =
" 1   fedora28   running  \n"
" 2   rhel7.5    running  \n";
    const char *exp2 =
" Id   Name       State    \n"
"--------------------------\n"
" 1    fedora28   running  \n"
" 2    rhel7.5    running  \n";

    vshTablePtr table = vshTableNew("Id", "Name", "State",
                                    NULL); //to ask about return
    if (!table)
        goto cleanup;

    vshTableRowAppend(table, "1", "fedora28", "running", NULL);
    vshTableRowAppend(table, "2", "rhel7.5", "running",
                      NULL);

    act = vshTablePrintToString(table, false);
    if (virTestCompareToString(exp, act) < 0)
        ret = -1;

    VIR_FREE(act);
    act = vshTablePrintToString(table, true);
    if (virTestCompareToString(exp2, act) < 0)
        ret = -1;

 cleanup:
    VIR_FREE(act);
    vshTableFree(table);
    return ret;
}

static int
testVshTableRowAppend(const void *opaque ATTRIBUTE_UNUSED)
{
    int ret = 0;

    vshTablePtr table = vshTableNew("Id", "Name", NULL);
    if (!table)
        goto cleanup;

    if (vshTableRowAppend(table, NULL) >= 0) {
        fprintf(stderr, "Appending NULL shouldn't work\n");
        ret = -1;
    }

    if (vshTableRowAppend(table, "2", NULL) >= 0) {
        fprintf(stderr, "Appending less items than in header\n");
        ret = -1;
    }

    if (vshTableRowAppend(table, "2", "rhel7.5", "running",
                      NULL) >= 0) {
        fprintf(stderr, "Appending more items than in header\n");
        ret = -1;
    }

    if (vshTableRowAppend(table, "2", "rhel7.5", NULL) < 0) {
        fprintf(stderr, "Appending same number of items as in header"
                        " should not return NULL\n");
        ret = -1;
    }

 cleanup:
    vshTableFree(table);
    return ret;
}

static int
testUnicode(const void *opaque ATTRIBUTE_UNUSED)
{

    int ret = 0;
    char *act = NULL;

    const char *exp =
" Id   名稱                  государство  \n"
"-----------------------------------------\n"
" 1    fedora28              running      \n"
" 2    つへソrhel7.5つへソ   running      \n";
    vshTablePtr table;

    table = vshTableNew("Id", "名稱", "государство", NULL);
    if (!table)
        goto cleanup;

    vshTableRowAppend(table, "1", "fedora28", "running", NULL);
    vshTableRowAppend(table, "2", "つへソrhel7.5つへソ", "running",
                      NULL);

    act = vshTablePrintToString(table, true);
    if (virTestCompareToString(exp, act) < 0)
        ret = -1;

 cleanup:
    VIR_FREE(act);
    vshTableFree(table);
    return ret;
}

/* Point of this test is to see how table behaves with right to left writing*/
static int
testUnicodeArabic(const void *opaque ATTRIBUTE_UNUSED)
{

    int ret = 0;
{'fraction_non_alphanumeric': 0.13348444780254376, 'fraction_numerical': 0.016370734164462913, 'mean_word_length': 2.358139534883721, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1031033', 'n_tokens_mistral': 2700, 'n_tokens_neox': 2614, 'n_words': 812}