<commit_before>/* Unit tests for gprintf
 * Copyright (C) 1995-1997  <NAME>, <NAME> and <NAME>
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work 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.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "glib.h"
#include "gstdio.h"

static void
test_retval_and_trunc (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 0, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (NULL, 0, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (buf, 5, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (buf, 1, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[0] == '\0');
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 2, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[1] == '\0');
  g_assert_cmpstr (buf, ==, "a");

  res = g_snprintf (buf, 3, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[2] == '\0');
  g_assert_cmpstr (buf, ==, "ab");

  res = g_snprintf (buf, 4, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[3] == '\0');
  g_assert_cmpstr (buf, ==, "abc");

  res = g_snprintf (buf, 5, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[3] == '\0');
  g_assert_cmpstr (buf, ==, "abc");
}

static void
test_d (void)
{
  gchar buf[128];
  gint res;
  const gchar *fmt;

  /* %d basic formatting */

  res = g_snprintf (buf, 128, "%d", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%d", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");

  res = g_snprintf (buf, 128, "%.0d", 0);
  g_assert_cmpint (res, ==, 0);
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 128, "%.0d", 1);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "1");

  res = g_snprintf (buf, 128, "%.d", 2);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "2");

  res = g_snprintf (buf, 128, "%d", -1);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-1");

  res = g_snprintf (buf, 128, "%.3d", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%.3d", -5);
  g_assert_cmpint (res, ==, 4);
  g_assert_cmpstr (buf, ==, "-005");

  res = g_snprintf (buf, 128, "%5.3d", 5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  005");

  res = g_snprintf (buf, 128, "%-5.3d", -5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "-005 ");

  /* %d, length modifiers */

  res = g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  /* %d, flags */

  res = g_snprintf (buf, 128, "%-d", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%-+d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");

  res = g_snprintf (buf, 128, "%+-d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");

  res = g_snprintf (buf, 128, "%+d", -5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "% d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, " 5");

  res = g_snprintf (buf, 128, "% .0d", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, " ");

  res = g_snprintf (buf, 128, "%03d", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%03d", -5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "-05");

  /* gcc emits warnings for the following formats, since the C spec
   * says some of the flags must be ignored. (The " " in "% +d" and
   * the "0" in "%-03d".) But we need to test that our printf gets
   * those rules right. So we fool gcc into not warning.
   */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
  fmt = "% +d";
  res = g_snprintf (buf, 128, fmt, 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");<commit_msg>clang fixes: tweak last commit<commit_after>/* Unit tests for gprintf
 * Copyright (C) 1995-1997  <NAME>, <NAME> and <NAME>
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work 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.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "glib.h"
#include "gstdio.h"

static void
test_retval_and_trunc (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 0, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (NULL, 0, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (buf, 5, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (buf, 1, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[0] == '\0');
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 2, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[1] == '\0');
  g_assert_cmpstr (buf, ==, "a");

  res = g_snprintf (buf, 3, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[2] == '\0');
  g_assert_cmpstr (buf, ==, "ab");

  res = g_snprintf (buf, 4, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[3] == '\0');
  g_assert_cmpstr (buf, ==, "abc");

  res = g_snprintf (buf, 5, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[3] == '\0');
  g_assert_cmpstr (buf, ==, "abc");
}

static void
test_d (void)
{
  gchar buf[128];
  gint res;
  const gchar *fmt;

  /* %d basic formatting */

  res = g_snprintf (buf, 128, "%d", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%d", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");

  res = g_snprintf (buf, 128, "%.0d", 0);
  g_assert_cmpint (res, ==, 0);
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 128, "%.0d", 1);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "1");

  res = g_snprintf (buf, 128, "%.d", 2);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "2");

  res = g_snprintf (buf, 128, "%d", -1);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-1");

  res = g_snprintf (buf, 128, "%.3d", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%.3d", -5);
  g_assert_cmpint (res, ==, 4);
  g_assert_cmpstr (buf, ==, "-005");

  res = g_snprintf (buf, 128, "%5.3d", 5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  005");

  res = g_snprintf (buf, 128, "%-5.3d", -5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "-005 ");

  /* %d, length modifiers */

  res = g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  /* %d, flags */

  res = g_snprintf (buf, 128, "%-d", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%-+d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");

  res = g_snprintf (buf, 128, "%+-d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");

  res = g_snprintf (buf, 128, "%+d", -5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "% d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, " 5");

  res = g_snprintf (buf, 128, "% .0d", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, " ");

  res = g_snprintf (buf, 128, "%03d", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%03d", -5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "-05");

  /* gcc emits warnings for the following formats, since the C spec
   * says some of the flags must be ignored. (The " " in "% +d" and
   * the "0" in "%-03d".) But we need to test that our printf gets
   * those rules right. So we fool gcc into not warning.
   */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
  fmt = "% +d";
  res = g_snprintf (buf, 128, fmt, 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");
{'fraction_non_alphanumeric': 0.19649737302977233, 'fraction_numerical': 0.043957968476357266, 'mean_word_length': 3.818987341772152, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5596272', 'n_tokens_mistral': 5102, 'n_tokens_neox': 4635, 'n_words': 1309}