Merge
[c11concurrency-benchmarks.git] / silo / masstree / test_string.cc
1 /* Masstree
2  * Eddie Kohler, Yandong Mao, Robert Morris
3  * Copyright (c) 2012-2013 President and Fellows of Harvard College
4  * Copyright (c) 2012-2013 Massachusetts Institute of Technology
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, subject to the conditions
9  * listed in the Masstree LICENSE file. These conditions include: you must
10  * preserve this copyright notice, and you cannot mention the copyright
11  * holders in advertising related to the Software without their permission.
12  * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13  * notice is a summary of the Masstree LICENSE file; the license in that file
14  * is legally binding.
15  */
16 #include "string.hh"
17 #include <stdio.h>
18 #include <assert.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include "straccum.hh"
22
23 template <typename T>
24 static bool
25 check_straccum_utf8(StringAccum &sa, const char *in, int inlen,
26                     const char *out, int outlen)
27 {
28     sa.clear();
29     Encoding::UTF8Encoder<T> encoder;
30     sa.append_encoded(encoder, in, in + inlen);
31     return sa.length() == outlen && memcmp(sa.begin(), out, sa.length()) == 0;
32 }
33
34 template <typename T>
35 static bool
36 check_straccum2_utf8(StringAccum &sa, const char *in, int inlen,
37                      const char *out, int outlen)
38 {
39     sa.clear();
40     memcpy(sa.reserve(inlen), in, inlen);
41     Encoding::UTF8Encoder<T> encoder;
42     sa.append_encoded(encoder, sa.begin(), sa.begin() + inlen);
43     return sa.length() == outlen && memcmp(sa.begin(), out, sa.length()) == 0;
44 }
45
46 int
47 main(int argc, char *argv[])
48 {
49     assert(String("abc").to_utf8() == "abc");
50     assert(String("").to_utf8() == "");
51     assert(String("ab\000cd", 5).to_utf8() == "abcd");
52     assert(String("\xc3\x9dHi!").to_utf8() == "\xc3\x9dHi!");
53     assert(String("\xddHi!").to_utf8() == "\xc3\x9dHi!");
54     assert(String("\xc3\x9dHi!\x9c").to_utf8() == "\xc3\x9dHi!\xc5\x93");
55     assert(String("ab\000c\x9c", 5).to_utf8() == "abc\xc5\x93");
56     assert(String("\xc3\x9dXY\000c\x9c", 7).to_utf8() == "\xc3\x9dXYc\xc5\x93");
57
58     StringAccum sa;
59     check_straccum_utf8<Encoding::UTF8>(sa, "abc", 3, "abc", 3);
60     check_straccum_utf8<Encoding::UTF8>(sa, "", 0, "", 0);
61     check_straccum_utf8<Encoding::UTF8>(sa, "ab\000cd", 5, "ab\000cd", 5);
62     check_straccum_utf8<Encoding::UTF8NoNul>(sa, "ab\000cd", 5, "abcd", 4);
63     check_straccum_utf8<Encoding::UTF8>(sa, "\xc3\x9dHi!", 5, "\xc3\x9dHi!", 5);
64     check_straccum_utf8<Encoding::Windows1252>(sa, "\xddHi!", 4, "\xc3\x9dHi!", 5);
65
66     check_straccum2_utf8<Encoding::UTF8>(sa, "abc", 3, "abc", 3);
67     check_straccum2_utf8<Encoding::UTF8>(sa, "", 0, "", 0);
68     check_straccum2_utf8<Encoding::UTF8>(sa, "ab\000cd", 5, "ab\000cd", 5);
69     check_straccum2_utf8<Encoding::UTF8NoNul>(sa, "ab\000cd", 5, "abcd", 4);
70     check_straccum2_utf8<Encoding::UTF8>(sa, "\xc3\x9dHi!", 5, "\xc3\x9dHi!", 5);
71     check_straccum2_utf8<Encoding::Windows1252>(sa, "\xddHi!", 4, "\xc3\x9dHi!", 5);
72
73     if (argc == 2) {
74         FILE *f;
75         if (strcmp(argv[1], "-") == 0)
76             f = stdin;
77         else if (!(f = fopen(argv[1], "rb"))) {
78             perror("test_string");
79             exit(1);
80         }
81         StringAccum sa;
82         while (!feof(f)) {
83             size_t x = fread(sa.reserve(1024), 1, 1024, f);
84             sa.adjust_length(x);
85         }
86         String s = sa.take_string().to_utf8(String::utf_strip_bom);
87         fwrite(s.data(), 1, s.length(), stdout);
88     }
89 }