xpcom/tests/TestTextFormatter.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestTextFormatter.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,40 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <nsTextFormatter.h>
    1.10 +#include <nsStringGlue.h>
    1.11 +#include <stdio.h>
    1.12 +
    1.13 +int main() 
    1.14 +{  
    1.15 +  int test_ok = true;
    1.16 +
    1.17 +  nsAutoString fmt(NS_LITERAL_STRING("%3$s %4$S %1$d %2$d %2$d %3$s"));
    1.18 +  char utf8[] = "Hello"; 
    1.19 +  char16_t ucs2[]={'W', 'o', 'r', 'l', 'd', 0x4e00, 0xAc00, 0xFF45, 0x0103, 0x00}; 
    1.20 +  int d=3; 
    1.21 +
    1.22 +  char16_t buf[256]; 
    1.23 +  nsTextFormatter::snprintf(buf, 256, fmt.get(), d, 333, utf8, ucs2); 
    1.24 +  nsAutoString out(buf); 
    1.25 +  if(strcmp("Hello World", NS_LossyConvertUTF16toASCII(out).get()))
    1.26 +    test_ok = false;
    1.27 +
    1.28 +  const char16_t *uout = out.get(); 
    1.29 +  const char16_t expected[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20,
    1.30 +                                0x57, 0x6F, 0x72, 0x6C, 0x64, 0x4E00,
    1.31 +                                0xAC00, 0xFF45, 0x0103, 0x20, 0x33,
    1.32 +                                0x20, 0x33, 0x33, 0x33, 0x20, 0x33,
    1.33 +                                0x33, 0x33, 0x20, 0x48, 0x65, 0x6C,
    1.34 +                                0x6C, 0x6F};
    1.35 +  for(uint32_t i=0;i<out.Length();i++) {
    1.36 +    if(uout[i] != expected[i]) {
    1.37 +      test_ok = false;
    1.38 +      break;
    1.39 +    }
    1.40 +  }
    1.41 +  printf(test_ok? "nsTextFormatter: OK\n": "nsTextFormatter: FAIL\n");
    1.42 +}
    1.43 +

mercurial