[haiku-commits] r38622 - haiku/trunk/src/tests/system/libroot/posix

  • From: zooey@xxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 12 Sep 2010 19:21:28 +0200 (CEST)

Author: zooey
Date: 2010-09-12 19:21:28 +0200 (Sun, 12 Sep 2010)
New Revision: 38622
Changeset: http://dev.haiku-os.org/changeset/38622

Modified:
   haiku/trunk/src/tests/system/libroot/posix/locale_test.cpp
Log:
* added tests for localized numerical formatting via printf()


Modified: haiku/trunk/src/tests/system/libroot/posix/locale_test.cpp
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/locale_test.cpp  2010-09-12 
15:34:08 UTC (rev 38621)
+++ haiku/trunk/src/tests/system/libroot/posix/locale_test.cpp  2010-09-12 
17:21:28 UTC (rev 38622)
@@ -1842,6 +1842,99 @@
 }
 
 
+// #pragma mark - printf 
-------------------------------------------------------
+
+
+struct sprintf_data {
+       const char* format;
+       double value;
+       const char* result;
+};
+
+
+void
+test_sprintf(const char* locale, const sprintf_data data[])
+{
+       setlocale(LC_ALL, locale);
+       printf("sprintf for '%s'\n", locale);
+
+       int problemCount = 0;
+       for(int i = 0; data[i].format != NULL; ++i) {
+               char buf[100];
+               if (strchr(data[i].format, 'd') != NULL)
+                       sprintf(buf, data[i].format, (int)data[i].value);
+               else if (strchr(data[i].format, 'f') != NULL)
+                       sprintf(buf, data[i].format, data[i].value);
+               if (strcmp(buf, data[i].result) != 0) {
+                       printf("\tPROBLEM: sprintf(\"%s\") = \"%s\" (expected 
\"%s\")\n",
+                               data[i].format, buf, data[i].result);
+                       problemCount++;
+               }
+       }
+       if (problemCount)
+               printf("\t%d problem(s) found!\n", problemCount);
+       else
+               printf("\tall fine\n");
+}
+
+
+void
+test_sprintf()
+{
+       const sprintf_data sprintf_posix[] = {
+               { "%d", 123, "123" },
+               { "%d", -123, "-123" },
+               { "%d", 123456, "123456" },
+               { "%'d", 123456, "123456" },
+               { "%f", 123, "123.000000" },
+               { "%f", -123, "-123.000000" },
+               { "%.2f", 123456.789, "123456.79" },
+               { "%'.2f", 123456.789, "123456.79" },
+               { NULL, NULL }
+       };
+       test_sprintf("POSIX", sprintf_posix);
+
+       const sprintf_data sprintf_de[] = {
+               { "%d", 123, "123" },
+               { "%d", -123, "-123" },
+               { "%d", 123456, "123456" },
+               { "%'d", 123456, "123.456" },
+               { "%f", 123, "123,000000" },
+               { "%f", -123, "-123,000000" },
+               { "%.2f", 123456.789, "123456,79" },
+               { "%'.2f", 123456.789, "123.456,79" },
+               { NULL, NULL }
+       };
+       test_sprintf("de_DE.UTF-8", sprintf_de);
+
+       const sprintf_data sprintf_gu[] = {
+               { "%d", 123, "123" },
+               { "%d", -123, "-123" },
+               { "%d", 123456, "123456" },
+               { "%'d", 123456, "123,456" },
+               { "%f", 123, "123.000000" },
+               { "%f", -123, "-123.000000" },
+               { "%.2f", 123456.789, "123456.79" },
+               { "%'.2f", 123456.789, "1,23,456.79" },
+               { NULL, NULL }
+       };
+       test_sprintf("gu_IN", sprintf_gu);
+
+       const sprintf_data sprintf_nb[] = {
+               { "%d", 123, "123" },
+               { "%d", -123, "-123" },
+               { "%d", 123456, "123456" },
+               { "%'d", 123456, "123 456" },
+               { "%f", 123, "123,000000" },
+               { "%f", -123, "-123,000000" },
+               { "%.2f", 123456.789, "123456,79" },
+               { "%'.2f", 123456.789, "123 456,79" },
+               { NULL, NULL }
+       };
+       test_sprintf("nb_NO", sprintf_nb);
+}
+
+
 // #pragma mark - main 
---------------------------------------------------------
 
 
@@ -1861,6 +1954,7 @@
        test_langinfo();
        test_collation();
        test_timeconversions();
+       test_sprintf();
 
        return 0;
 }


Other related posts:

  • » [haiku-commits] r38622 - haiku/trunk/src/tests/system/libroot/posix - zooey