int main()

in testing/scanftest/scanftest_main.c [1071:1466]


int main(int argc, FAR char *argv[])
{
  int t;
  int i;
  int c;
  int n1 = 12345;
  int n2;
  bool ok;
  char s1[84];
  char s2[80];
  float f1;
  float f2;
  double d1;
  double d2;
  FAR FILE *fp;

  FAR const char *teststring = "teststring a";
  FAR const char *fname = "/mnt/fs/test.txt";

  /* Test that scanf() can recognize percent-signs in the input. ** Test that
   * integer converters skip white-space. ** Test that "%i" can scan a single
   * zero digit (followed by EOF).
   */

  sscanf("%  \n\f\v\t  0", "%%%i", &n1);
  if (n1 != 0)
    {
      printf("sscanf()'s \"%%%%%%i\" couldn't scan either a \"%%\" "
             "or a single zero digit.\n\n");
    }

  /* Test scanf()'s return-value: EOF if input ends before the first *
   * conversion-attempt begins; an assignment-count, otherwise. * Test that
   * scanf() properly converts and assigns the correct number * of arguments.
   */

  for (i = 0; i < 2; i++)
    {
      if (i)
        {
          char s3[3];

          printf("\nBack to Back Test...\n");

          memset(s1, '\0', sizeof s1);
          memset(s2, '\0', sizeof s2);
          memset(s3, '\0', sizeof s3);

          fp = fopen(fname, "wb");
          if (fp)
            {
              fputs(teststring, fp);
              fclose(fp);
              fp = fopen(fname, "rb");
              if (fp != NULL)
                {
                  fscanf(fp, "%s", s2);
                  fscanf(fp, "%2c", s3);
                  snprintf(s1, sizeof(s1), "%s%s", s2, s3);

                  if (strcmp(s1, teststring) != 0)
                    {
                      printf("Error %s != %s.\n", teststring, s1);
                    }
                  else
                    {
                      printf("Test PASSED.\n");
                    }
                }
              else
                {
                  printf("Error opening %s for read.\n", fname);
                }
            }
          else
            {
              printf("Error opening %s for write.\n", fname);
            }
        }

      printf("\nTesting %cscanf()'s return-value,\nconversions, and "
             "assignments...\n",
             i ? 'f' : 's');

      for (t = 0; t < ARRAYSIZE(test_data); ++t)
        {
          /* Prefill the arguments with zeroes. */

          f1 = f2 = d1 = d2 = n1 = n2 = 0;
          memset(s1, '\0', sizeof s1);
          memset(s2, '\0', sizeof s2);

          ok = true;

          if (i)
            {
              fp = fopen(fname, "wb");
              if (fp)
                {
                  fputs(test_data[t].input, fp);
                  fclose(fp);
                }
              else
                {
                  printf("Error opening %s for write.\n", fname);
                  break;
                }

              fp = fopen(fname, "rb");
              if (fp)
                {
                  c = fscanf
                  (fp, test_data[t].format,

                  /* Avoid warning messages about different  pointer-
                   * types, by casting them to void-pointers.
                   */

                  test_data[t].type1 == INT ? (FAR void *)&n1 :
                    test_data[t].type1 == FLOAT ? (FAR void *)&f1 :
                      test_data[t].type1 == DOUBLE ? (FAR void *)&d1 :
                        (FAR void *)s1,
                  test_data[t].type2 == INT ? (FAR void *)&n2 :
                    test_data[t].type2 == FLOAT ? (FAR void *)&f2 :
                      test_data[t].type2 == DOUBLE ? (FAR void *)&d2 :
                        (FAR void *)s2
                  );

                  fclose(fp);
                }
              else
                {
                  printf("Error opening %s for read.\n", fname);
                  break;
                }
            }
          else
            {
              c = sscanf
              (test_data[t].input, test_data[t].format,

              /* Avoid warning messages about different pointer-types, by
               * casting them to void-pointers.
               */

              test_data[t].type1 == INT ? (FAR void *)&n1 :
                test_data[t].type1 == FLOAT ? (FAR void *)&f1 :
                  test_data[t].type1 == DOUBLE ? (FAR void *)&d1 :
                    (FAR void *)s1,
              test_data[t].type2 == INT ? (FAR void *)&n2 :
                test_data[t].type2 == FLOAT ? (FAR void *)&f2 :
                  test_data[t].type2 == DOUBLE ? (FAR void *)&d2 :
                    (FAR void *)s2
              );
            }

          if (c != test_data[t].rvalue)
            {
              printf("Test #%u returned %d instead of %d.\n", t + 1, c,
                     test_data[t].rvalue);
              ok = false;
            }

          if (test_data[t].type1 == INT)
            {
              if (test_data[t].v1.nvalue != n1)
                {
                  printf("Test #%u assigned %i, instead of %i,\n"
                         "\tto the first argument.\n\n", t + 1, n1,
                         test_data[t].v1.nvalue);
                  ok = false;
                }
            }
          else if (test_data[t].type1 == FLOAT)
            {
              if (test_data[t].v1.fvalue != f1)
                {
                  printf("Test #%u assigned %e, instead of %e,\n"
                         "\tto the first argument.\n\n", t + 1, f1,
                         test_data[t].v1.fvalue);
                  ok = false;
                }
            }
          else if (test_data[t].type1 == DOUBLE)
            {
              if (test_data[t].v1.dvalue != d1)
                {
                  printf("Test #%u assigned %le, instead of %le,\n"
                         "\tto the first argument.\n\n", t + 1, d1,
                         test_data[t].v1.dvalue);
                  ok = false;
                }
            }
          else
            {
              /* test_data[t].type1 == CHAR */

              if (strcmp(test_data[t].v1.svalue, s1))
                {
                  printf("Test #%u assigned\n\"%s\",\n"
                         "\tinstead of\n\"%s\",\n"
                         "\tto the first argument.\n\n", t + 1, s1,
                         test_data[t].v1.svalue);
                  ok = false;
                }
            }

          if (test_data[t].type2 == INT)
            {
              if (test_data[t].v2.nvalue != n2)
                {
                  printf("Test #%u assigned %i, instead of %i,\n"
                         "\tto the second argument.\n\n", t + 1, n2,
                         test_data[t].v2.nvalue);
                  ok = false;
                }
            }
          else if (test_data[t].type2 == FLOAT)
            {
              if (test_data[t].v2.fvalue != f2)
                {
                  printf("Test #%u assigned %e, instead of %e,\n"
                         "\tto the second argument.\n\n", t + 1, f2,
                         test_data[t].v2.fvalue);
                  ok = false;
                }
            }
          else if (test_data[t].type2 == DOUBLE)
            {
              if (test_data[t].v2.dvalue != d2)
                {
                  printf("Test #%u assigned %le, instead of %le,\n"
                         "\tto the second argument.\n\n", t + 1, d2,
                         test_data[t].v2.dvalue);
                  ok = false;
                }
            }
          else
            {
              /* test_data[t].type2 == CHAR */

              if (strcmp(test_data[t].v2.svalue, s2))
                {
                  printf("Test #%u assigned\n\"%s\",\n"
                         "\tinstead of\n\"%s\",\n"
                         "\tto the second argument.\n\n", t + 1, s2,
                         test_data[t].v2.svalue);
                  ok = false;
                }
            }

          if (ok)
            {
              printf("Test #%u PASSED.\n", t + 1);
            }
        }
    }

  /* Test the char, short, and long specification-modifiers. */

  printf("\nTesting scanf()'s type-modifiers...\n");
  for (t = 0; t < ARRAYSIZE(type_data); ++t)
    {
      unsigned char hhu;
      unsigned short hu;
      unsigned int nou;
      unsigned long lu;
      unsigned long long llu;
      signed char hhs;
      signed short hs;
      signed int nos;
      signed long ls;
      signed long long lls;

      ok = true;
      switch (type_data[t].type)
        {
        case HH_MOD_S:
          hhs = 0L;
          sscanf(type_data[t].input, type_data[t].format, &hhs);
          if (type_data[t].value.s != hhs)
            {
              printf("Test #%u assigned %hhd instead of %lli.\n", t + 1,
                     hhs, type_data[t].value.s);
              ok = false;
            }
          break;

        case HH_MOD_U:
          hhu = 0L;
          sscanf(type_data[t].input, type_data[t].format, &hhu);
          if (type_data[t].value.u != hhu)
            {
              printf("Test #%u assigned %hhu instead of %lli.\n",
                     t + 1, hhu, type_data[t].value.u);
              ok = false;
            }
          break;

        case H_MOD_S:
          hs = 0L;
          sscanf(type_data[t].input, type_data[t].format, &hs);
          if (type_data[t].value.s != hs)
            {
              printf("Test #%u assigned %hd instead of %lli.\n",
                     t + 1, hs, type_data[t].value.s);
              ok = false;
            }
          break;

        case H_MOD_U:
          hu = 0L;
          sscanf(type_data[t].input, type_data[t].format, &hu);
          if (type_data[t].value.u != hu)
            {
              printf("Test #%u assigned %hu instead of %lli.\n",
                     t + 1, hu, type_data[t].value.u);
              ok = false;
            }
          break;

        case NO_MOD_S:
          nos = 0L;
          sscanf(type_data[t].input, type_data[t].format, &nos);
          if (type_data[t].value.s != nos)
            {
              printf("Test #%u assigned %d instead of %lli.\n",
                     t + 1, nos, type_data[t].value.s);
              ok = false;
            }
          break;

        case NO_MOD_U:
          nou = 0L;
          sscanf(type_data[t].input, type_data[t].format, &nou);
          if (type_data[t].value.u != nou)
            {
              printf("Test #%u assigned %u instead of %lli.\n",
                     t + 1, nou, type_data[t].value.u);
              ok = false;
            }
          break;

        case L_MOD_S:
          ls = 0L;
          sscanf(type_data[t].input, type_data[t].format, &ls);
          if (type_data[t].value.s != ls)
            {
              printf("Test #%u assigned %ld instead of %lli.\n",
                     t + 1, ls, type_data[t].value.s);
              ok = false;
            }
          break;

        case L_MOD_U:
          lu = 0L;
          sscanf(type_data[t].input, type_data[t].format, &lu);
          if (type_data[t].value.u != lu)
            {
              printf("Test #%u assigned %lu instead of %lli.\n",
                     t + 1, lu, type_data[t].value.u);
              ok = false;
            }
          break;

        case LL_MOD_S:
          lls = 0L;
          sscanf(type_data[t].input, type_data[t].format, &lls);
          if (type_data[t].value.s != lls)
            {
              printf("Test #%u assigned %lld instead of %lli.\n",
                     t + 1, lls, type_data[t].value.s);
              ok = false;
            }
          break;

        case LL_MOD_U:
          llu = 0L;
          sscanf(type_data[t].input, type_data[t].format, &llu);
          if (type_data[t].value.u != llu)
            {
              printf("Test #%u assigned %llu instead of %lli.\n",
                     t + 1, llu, type_data[t].value.u);
              ok = false;
            }
          break;
        }

      if (ok)
        {
          printf("Test #%u PASSED.\n", t + 1);
        }
    }

  return OK;
}