the Compartmented Robust Posix C++ Unit Test system

crpcut::collate

Compare the sort order of strings in a locale.

Used in: An ASSERT_TRUE(expr), VERIFY_TRUE(expr), ASSERT_FALSE(expr) or VERIFY_FALSE(expr).

The basic use syntax is:

ASSERT_TRUE(crpcut::collate(reference_string, locale) op tested_string)

locale defaults to the current locale and can be left out.

reference_string can be either a C-string or std::string

tested_string can be either a C-string or std::string

op is one of:

operator

<

reference_string is sorted before tested_string in the locale.

<=

reference_string is sorted before, or equitable with, tested_string in the locale.

>

reference_string is sorted behind tested_string in the locale.

>=

reference_string is sorted behind, or equitable with, tested_string in the locale.

==

reference_string is equitable with tested_string in the sort order of the locale.

!=

reference_string is not equitable with tested_string in the sort order of the locale.

Example: The test program

     
     #include <crpcut.hpp>
     
     char str1_utf8[] = { '\xc3', '\xb6', 'z', 0 };
     char str2_utf8[] = { 'z', '\xc3', '\xb6', 0};
     
     
     TEST(in_german_locale)
     {
       ASSERT_TRUE(crpcut::collate(str1_utf8, std::locale("de_DE.utf8")) < str2_utf8);
       ASSERT_TRUE(crpcut::collate(str1_utf8, std::locale("de_DE.utf8")) > str2_utf8);
     }
     
     TEST(in_swedish_locale)
     {
       ASSERT_TRUE(crpcut::collate(str1_utf8, std::locale("sv_SE.utf8")) < str2_utf8);
       ASSERT_TRUE(crpcut::collate(str1_utf8, std::locale("sv_SE.utf8")) > str2_utf8);
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

      

fails the opposite tests in the different locales:


     FAILED!: in_german_locale
     phase="running"  --------------------------------------------------------------
     samples/collate.cpp:37
     ASSERT_TRUE(crpcut::collate(str1_utf8, std::locale("de_DE.utf8")) > str2_utf8)
       is evaluated as:
         Failed in locale "de_DE.utf8"
       with left hand value = "öz"
       and right hand value = "zö"
     -------------------------------------------------------------------------------
     ===============================================================================
     FAILED!: in_swedish_locale
     phase="running"  --------------------------------------------------------------
     samples/collate.cpp:42
     ASSERT_TRUE(crpcut::collate(str1_utf8, std::locale("sv_SE.utf8")) < str2_utf8)
       is evaluated as:
         Failed in locale "sv_SE.utf8"
       with left hand value = "öz"
       and right hand value = "zö"
     -------------------------------------------------------------------------------
     ===============================================================================
     2 test cases selected
     
                    Sum   Critical   Non-critical
     FAILED   :       2          2              0

      

crpcut::collate can also be used as a function template, crpcut::collate<translation>(), with:

translation

crpcut::lowercase

Translate reference_string and tested_string to lower case, in the locale, before collation order comparison.

crpcut::uppercase

Translate reference_string and tested_string to upper case, in the locale, before collation order comparison.

crpcut::verbatim

Do not translate reference_string nor tested_string before collation order comparison. This is the behaviour of the non-templated version of crpcut::collate()

Example: The test program

     
     #include <crpcut.hpp>
     
     TEST(case_insensitive_pass)
     {
       ASSERT_TRUE(crpcut::collate<crpcut::lowercase>("Name") == "name");
     }
     
     TEST(case_sensitive_fail)
     {
       ASSERT_TRUE(crpcut::collate<crpcut::verbatim>("Name") == "name");
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

      

fails one test:


     FAILED!: case_sensitive_fail
     phase="running"  --------------------------------------------------------------
     samples/collate_case.cpp:37
     ASSERT_TRUE(crpcut::collate<crpcut::verbatim>("Name") == "name")
       is evaluated as:
         Failed in locale "C"
       with left hand value = "Name"
       and right hand value = "name"
     -------------------------------------------------------------------------------
     ===============================================================================
     2 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       1          1              0