summaryrefslogtreecommitdiff
path: root/libs/pbd/test/string_convert_test.cc
blob: 2f06ad9c52868894a730ddf50f2a1d3a31112d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#include "string_convert_test.h"

#include <stdio.h>
#include <string.h>

#include <limits>
#include <cassert>

#include <pthread.h>

#include <glib.h>

#include "pbd/string_convert.h"

using namespace PBD;
using namespace std;

CPPUNIT_TEST_SUITE_REGISTRATION (StringConvertTest);

namespace {

class LocaleGuard {
public:
	// RAII class that sets the global C locale and then resets it to its
	// previous setting when going out of scope
	LocaleGuard (const std::string& locale)
	{
		m_previous_locale = setlocale (LC_ALL, NULL);

		CPPUNIT_ASSERT (m_previous_locale != NULL);

		const char* new_locale = setlocale (LC_ALL, locale.c_str ());

		if (new_locale == NULL) {
			std::cerr << "Failed to set locale to : " << locale << std::endl;
		}

		CPPUNIT_ASSERT (new_locale != NULL);
	}

	~LocaleGuard ()
	{
		CPPUNIT_ASSERT (setlocale (LC_ALL, m_previous_locale) != NULL);
	}

private:
	const char* m_previous_locale;
};

static
bool
check_decimal_mark_is_comma ()
{
	char buf[32];
	double const dnum = 12345.67890;
	snprintf (buf, sizeof(buf), "%.12g", dnum);
	bool found = (strchr (buf, ',') != NULL);
	return found;
}

static std::vector<std::string> get_locale_list ()
{
	std::vector<std::string> locales;

	locales.push_back(""); // User preferred locale

#ifdef PLATFORM_WINDOWS
	locales.push_back("French_France.1252"); // must be first
	locales.push_back("Dutch_Netherlands.1252");
	locales.push_back("Italian_Italy.1252");
	locales.push_back("Farsi_Iran.1256");
	locales.push_back("Chinese_China.936");
	locales.push_back("Czech_Czech Republic.1250");
#else
	locales.push_back("fr_FR"); // French France
	locales.push_back("fr_FR.UTF-8");
	locales.push_back("de_DE"); // German Germany
	locales.push_back("de_DE.UTF-8");
	locales.push_back("nl_NL"); // Dutch - Netherlands
	locales.push_back("nl_NL.UTF-8");
	locales.push_back("it_IT"); // Italian
	locales.push_back("fa_IR"); // Farsi Iran
	locales.push_back("zh_CN"); // Chinese
	locales.push_back("cs_CZ"); // Czech
#endif
	return locales;
}

static std::vector<std::string> get_supported_locales ()
{
	std::vector<std::string> locales = get_locale_list ();
	std::vector<std::string> supported_locales;

	const char * m_orig_locale = setlocale (LC_ALL, NULL);

	CPPUNIT_ASSERT (m_orig_locale != NULL);

	std::cerr << std::endl << "Original locale: " << m_orig_locale << std::endl;

	for (std::vector<std::string>::const_iterator it = locales.begin(); it != locales.end(); ++it) {

		const char* locale = it->c_str();
		const char* new_locale = setlocale (LC_ALL, locale);

		if (new_locale == NULL) {
			std::cerr << "Unable to set locale : " << locale << ", may not be installed." << std::endl;
			continue;
		}

		if (*it != new_locale) {
			// Setting the locale may be successful but locale has a different
			// (or longer) name.
			if (it->empty()) {
				std::cerr << "User preferred locale is : " << new_locale << std::endl;
			} else {
				std::cerr << "locale : " << locale << ", has name : " << new_locale << std::endl;
			}
		}

		std::cerr << "Adding locale : " << new_locale << " to test locales" << std::endl;

		supported_locales.push_back (*it);
	}

	if (setlocale (LC_ALL, m_orig_locale) == NULL) {
		std::cerr << "ERROR: Unable to restore original locale " << m_orig_locale
		          << ", further tests may be invalid." << std::endl;
	}

	return supported_locales;
}

static std::vector<std::string> get_test_locales ()
{
	static std::vector<std::string> locales = get_supported_locales ();
	return locales;
}

static bool get_locale_with_comma_decimal_mark (std::string& locale_str) {
	std::vector<std::string> locales = get_test_locales ();

	for (std::vector<std::string>::const_iterator it = locales.begin(); it != locales.end(); ++it) {
		LocaleGuard guard (*it);
		if (check_decimal_mark_is_comma ()) {
			locale_str = *it;
			return true;
		}
	}
	return false;
}

} // anon namespace

void
StringConvertTest::test_required_locales ()
{
	std::string locale_str;
	CPPUNIT_ASSERT(get_locale_with_comma_decimal_mark(locale_str));
}

static const std::string MAX_INT16_STR ("32767");
static const std::string MIN_INT16_STR ("-32768");

typedef void (*TestFunctionType)(void);

void
test_function_for_locales (TestFunctionType test_func)
{
	const std::vector<std::string> locales = get_test_locales();

	for (std::vector<std::string>::const_iterator ci = locales.begin ();
	     ci != locales.end ();
	     ++ci) {
		LocaleGuard guard (*ci);
		test_func ();
	}
}

void
_test_int16_conversion ()
{
	string str;
	CPPUNIT_ASSERT (int16_to_string (numeric_limits<int16_t>::max (), str));
	CPPUNIT_ASSERT_EQUAL (MAX_INT16_STR, str);

	int16_t val = 0;
	CPPUNIT_ASSERT (string_to_int16 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<int16_t>::max (), val);

	CPPUNIT_ASSERT (int16_to_string (numeric_limits<int16_t>::min (), str));
	CPPUNIT_ASSERT_EQUAL (MIN_INT16_STR, str);

	CPPUNIT_ASSERT (string_to_int16 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<int16_t>::min (), val);

	// test the string_to/to_string templates
	int16_t max = numeric_limits<int16_t>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<int16_t>(to_string (max)));

	int16_t min = numeric_limits<int16_t>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<int16_t>(to_string (min)));
}

void
StringConvertTest::test_int16_conversion ()
{
	test_function_for_locales(&_test_int16_conversion);
}

static const std::string MAX_UINT16_STR("65535");
static const std::string MIN_UINT16_STR("0");

void
_test_uint16_conversion ()
{
	string str;
	CPPUNIT_ASSERT (uint16_to_string (numeric_limits<uint16_t>::max (), str));
	CPPUNIT_ASSERT_EQUAL (MAX_UINT16_STR, str);

	uint16_t val = 0;
	CPPUNIT_ASSERT (string_to_uint16 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<uint16_t>::max (), val);

	CPPUNIT_ASSERT (uint16_to_string (numeric_limits<uint16_t>::min (), str));
	CPPUNIT_ASSERT_EQUAL (MIN_UINT16_STR, str);

	CPPUNIT_ASSERT (string_to_uint16 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<uint16_t>::min (), val);

	// test the string_to/to_string templates
	uint16_t max = numeric_limits<uint16_t>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<uint16_t>(to_string (max)));

	uint16_t min = numeric_limits<uint16_t>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<uint16_t>(to_string (min)));
}

void
StringConvertTest::test_uint16_conversion ()
{
	test_function_for_locales(&_test_uint16_conversion);
}

static const std::string MAX_INT32_STR ("2147483647");
static const std::string MIN_INT32_STR ("-2147483648");

void
_test_int32_conversion ()
{
	string str;
	CPPUNIT_ASSERT (int32_to_string (numeric_limits<int32_t>::max (), str));
	CPPUNIT_ASSERT_EQUAL (MAX_INT32_STR, str);

	int32_t val = 0;
	CPPUNIT_ASSERT (string_to_int32 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<int32_t>::max (), val);

	CPPUNIT_ASSERT (int32_to_string (numeric_limits<int32_t>::min (), str));
	CPPUNIT_ASSERT_EQUAL (MIN_INT32_STR, str);

	CPPUNIT_ASSERT (string_to_int32 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<int32_t>::min (), val);

	// test the string_to/to_string templates
	int32_t max = numeric_limits<int32_t>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<int32_t>(to_string (max)));

	int32_t min = numeric_limits<int32_t>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<int32_t>(to_string (min)));
}

void
StringConvertTest::test_int32_conversion ()
{
	test_function_for_locales(&_test_int32_conversion);
}

static const std::string MAX_UINT32_STR("4294967295");
static const std::string MIN_UINT32_STR("0");

void
_test_uint32_conversion ()
{
	string str;
	CPPUNIT_ASSERT (uint32_to_string (numeric_limits<uint32_t>::max (), str));
	CPPUNIT_ASSERT_EQUAL (MAX_UINT32_STR, str);

	uint32_t val = 0;
	CPPUNIT_ASSERT (string_to_uint32 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<uint32_t>::max (), val);

	CPPUNIT_ASSERT (uint32_to_string (numeric_limits<uint32_t>::min (), str));
	CPPUNIT_ASSERT_EQUAL (MIN_UINT32_STR, str);

	CPPUNIT_ASSERT (string_to_uint32 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<uint32_t>::min (), val);

	// test the string_to/to_string templates
	uint32_t max = numeric_limits<uint32_t>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<uint32_t>(to_string (max)));

	uint32_t min = numeric_limits<uint32_t>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<uint32_t>(to_string (min)));
}

void
StringConvertTest::test_uint32_conversion ()
{
	test_function_for_locales(&_test_uint32_conversion);
}

static const std::string MAX_INT64_STR ("9223372036854775807");
static const std::string MIN_INT64_STR ("-9223372036854775808");

void
_test_int64_conversion ()
{
	string str;
	CPPUNIT_ASSERT (int64_to_string (numeric_limits<int64_t>::max (), str));
	CPPUNIT_ASSERT_EQUAL (MAX_INT64_STR, str);

	int64_t val = 0;
	CPPUNIT_ASSERT (string_to_int64 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<int64_t>::max (), val);

	CPPUNIT_ASSERT (int64_to_string (numeric_limits<int64_t>::min (), str));
	CPPUNIT_ASSERT_EQUAL (MIN_INT64_STR, str);

	CPPUNIT_ASSERT (string_to_int64 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<int64_t>::min (), val);

	// test the string_to/to_string templates
	int64_t max = numeric_limits<int64_t>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<int64_t>(to_string (max)));

	int64_t min = numeric_limits<int64_t>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<int64_t>(to_string (min)));
}

void
StringConvertTest::test_int64_conversion ()
{
	test_function_for_locales(&_test_int64_conversion);
}

static const std::string MAX_UINT64_STR ("18446744073709551615");
static const std::string MIN_UINT64_STR ("0");

void
_test_uint64_conversion ()
{
	string str;
	CPPUNIT_ASSERT (uint64_to_string (numeric_limits<uint64_t>::max (), str));
	CPPUNIT_ASSERT_EQUAL (MAX_UINT64_STR, str);

	uint64_t val = 0;
	CPPUNIT_ASSERT (string_to_uint64 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<uint64_t>::max (), val);

	CPPUNIT_ASSERT (uint64_to_string (numeric_limits<uint64_t>::min (), str));
	CPPUNIT_ASSERT_EQUAL (MIN_UINT64_STR, str);

	CPPUNIT_ASSERT (string_to_uint64 (str, val));
	CPPUNIT_ASSERT_EQUAL (numeric_limits<uint64_t>::min (), val);

	// test the string_to/to_string templates
	uint64_t max = numeric_limits<uint64_t>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<uint64_t>(to_string (max)));

	uint64_t min = numeric_limits<uint64_t>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<uint64_t>(to_string (min)));
}

void
StringConvertTest::test_uint64_conversion ()
{
	test_function_for_locales(&_test_uint64_conversion);
}

static const std::string POS_INFINITY_STR ("infinity");
static const std::string NEG_INFINITY_STR ("-infinity");
static const std::string POS_INFINITY_CAPS_STR ("INFINITY");
static const std::string NEG_INFINITY_CAPS_STR ("-INFINITY");
static const std::string POS_INF_STR ("inf");
static const std::string NEG_INF_STR ("-inf");
static const std::string POS_INF_CAPS_STR ("INF");
static const std::string NEG_INF_CAPS_STR ("-INF");

static
std::vector<std::string>
_pos_infinity_strings ()
{
	std::vector<std::string> vec;
	vec.push_back (POS_INFINITY_STR);
	vec.push_back (POS_INFINITY_CAPS_STR);
	vec.push_back (POS_INF_STR);
	vec.push_back (POS_INF_CAPS_STR);
	return vec;
}

static
std::vector<std::string>
_neg_infinity_strings ()
{
	std::vector<std::string> vec;
	vec.push_back (NEG_INFINITY_STR);
	vec.push_back (NEG_INFINITY_CAPS_STR);
	vec.push_back (NEG_INF_STR);
	vec.push_back (NEG_INF_CAPS_STR);
	return vec;
}

template <class FloatType>
void
_test_infinity_conversion ()
{
	const FloatType pos_infinity = numeric_limits<FloatType>::infinity ();
	const FloatType neg_infinity = -numeric_limits<FloatType>::infinity ();

	// Check float -> string
	string str;
	CPPUNIT_ASSERT (to_string<FloatType> (pos_infinity, str));
	CPPUNIT_ASSERT_EQUAL (POS_INF_STR, str);

	CPPUNIT_ASSERT (to_string<FloatType> (neg_infinity, str));
	CPPUNIT_ASSERT_EQUAL (NEG_INF_STR, str);

	// Check string -> float for all supported string representations of "INFINITY"
	std::vector<std::string> pos_inf_strings = _pos_infinity_strings ();

	for (std::vector<std::string>::const_iterator i = pos_inf_strings.begin ();
	     i != pos_inf_strings.end (); ++i) {
		FloatType pos_infinity_arg;
		CPPUNIT_ASSERT (string_to<FloatType> (*i, pos_infinity_arg));
		CPPUNIT_ASSERT_EQUAL (pos_infinity_arg, pos_infinity);
	}

	// Check string -> float for all supported string representations of "-INFINITY"
	std::vector<std::string> neg_inf_strings = _neg_infinity_strings ();

	for (std::vector<std::string>::const_iterator i = neg_inf_strings.begin ();
	     i != neg_inf_strings.end (); ++i) {
		FloatType neg_infinity_arg;
		CPPUNIT_ASSERT (string_to<FloatType> (*i, neg_infinity_arg));
		CPPUNIT_ASSERT_EQUAL (neg_infinity_arg, neg_infinity);
	}

	// Check round-trip equality
	CPPUNIT_ASSERT_EQUAL (pos_infinity, string_to<FloatType> (to_string<FloatType> (pos_infinity)));
	CPPUNIT_ASSERT_EQUAL (neg_infinity, string_to<FloatType> (to_string<FloatType> (neg_infinity)));
}

static const std::string MAX_FLOAT_WIN ("3.4028234663852886e+038");
static const std::string MIN_FLOAT_WIN ("1.1754943508222875e-038");
static const std::string MAX_FLOAT_STR ("3.4028234663852886e+38");
static const std::string MIN_FLOAT_STR ("1.1754943508222875e-38");

void
_test_float_conversion ()
{
	// check float to string and back again for min and max float values
	string str;
	CPPUNIT_ASSERT (float_to_string (numeric_limits<float>::max (), str));
#ifdef PLATFORM_WINDOWS
	CPPUNIT_ASSERT_EQUAL (MAX_FLOAT_WIN, str);
#else
	CPPUNIT_ASSERT_EQUAL (MAX_FLOAT_STR, str);
#endif

	float val = 0.0f;
	CPPUNIT_ASSERT (string_to_float (str, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<float>::max (), val, numeric_limits<float>::epsilon ());

	CPPUNIT_ASSERT (float_to_string (numeric_limits<float>::min (), str));
#ifdef PLATFORM_WINDOWS
	CPPUNIT_ASSERT_EQUAL (MIN_FLOAT_WIN, str);
#else
	CPPUNIT_ASSERT_EQUAL (MIN_FLOAT_STR, str);
#endif

	CPPUNIT_ASSERT (string_to_float (str, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<float>::min (), val, numeric_limits<float>::epsilon ());

	// test the string_to/to_string templates
	float max = numeric_limits<float>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<float>(to_string<float> (max)));

	float min = numeric_limits<float>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<float>(to_string<float> (min)));

// check that parsing the windows float string representation with the
// difference in the exponent part parses correctly on other platforms
// and vice versa
#ifdef PLATFORM_WINDOWS
	CPPUNIT_ASSERT (string_to_float (MAX_FLOAT_STR, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<float>::max (), val, numeric_limits<float>::epsilon ());

	CPPUNIT_ASSERT (string_to_float (MIN_FLOAT_STR, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<float>::min (), val, numeric_limits<float>::epsilon ());
#else
	CPPUNIT_ASSERT (string_to_float (MAX_FLOAT_WIN, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<float>::max (), val, numeric_limits<float>::epsilon ());

	CPPUNIT_ASSERT (string_to_float (MIN_FLOAT_WIN, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<float>::min (), val, numeric_limits<float>::epsilon ());
#endif

	_test_infinity_conversion<float>();
}

void
StringConvertTest::test_float_conversion ()
{
	test_function_for_locales(&_test_float_conversion);
}

static const std::string MAX_DOUBLE_STR ("1.7976931348623157e+308");
static const std::string MIN_DOUBLE_STR ("2.2250738585072014e-308");

void
_test_double_conversion ()
{
	string str;
	CPPUNIT_ASSERT (double_to_string (numeric_limits<double>::max (), str));
	CPPUNIT_ASSERT_EQUAL (MAX_DOUBLE_STR, str);

	double val = 0.0;
	CPPUNIT_ASSERT (string_to_double (str, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<double>::max (), val, numeric_limits<double>::epsilon ());

	CPPUNIT_ASSERT (double_to_string (numeric_limits<double>::min (), str));
	CPPUNIT_ASSERT_EQUAL (MIN_DOUBLE_STR, str);

	CPPUNIT_ASSERT (string_to_double (str, val));
	CPPUNIT_ASSERT_DOUBLES_EQUAL (
	    numeric_limits<double>::min (), val, numeric_limits<double>::epsilon ());

	// test that overflow fails
	CPPUNIT_ASSERT (!string_to_double ("1.8e+308", val));
	// test that underflow fails
	CPPUNIT_ASSERT (!string_to_double ("2.4e-310", val));

	// test the string_to/to_string templates
	double max = numeric_limits<double>::max ();
	CPPUNIT_ASSERT_EQUAL (max, string_to<double>(to_string<double> (max)));

	double min = numeric_limits<double>::min ();
	CPPUNIT_ASSERT_EQUAL (min, string_to<double>(to_string<double> (min)));

	_test_infinity_conversion<double>();
}

void
StringConvertTest::test_double_conversion ()
{
	test_function_for_locales(&_test_double_conversion);
}

// we have to use these as CPPUNIT_ASSERT_EQUAL won't accept char arrays
static const std::string BOOL_TRUE_STR ("yes");
static const std::string BOOL_FALSE_STR ("no");

void
StringConvertTest::test_bool_conversion ()
{
	string str;

	// check the normal case for true/false
	CPPUNIT_ASSERT (bool_to_string (true, str));
	CPPUNIT_ASSERT_EQUAL (BOOL_TRUE_STR, str);

	bool val = false;
	CPPUNIT_ASSERT (string_to_bool (str, val));
	CPPUNIT_ASSERT_EQUAL (val, true);

	CPPUNIT_ASSERT (bool_to_string (false, str));
	CPPUNIT_ASSERT_EQUAL (BOOL_FALSE_STR, str);

	val = true;
	CPPUNIT_ASSERT (string_to_bool (str, val));
	CPPUNIT_ASSERT_EQUAL (val, false);

	// now check the other accepted values for true and false
	// when converting from a string to bool

	val = false;
	CPPUNIT_ASSERT (string_to_bool ("1", val));
	CPPUNIT_ASSERT_EQUAL (val, true);

	val = true;
	CPPUNIT_ASSERT (string_to_bool ("0", val));
	CPPUNIT_ASSERT_EQUAL (val, false);

	val = false;
	CPPUNIT_ASSERT (string_to_bool ("Y", val));
	CPPUNIT_ASSERT_EQUAL (val, true);

	val = true;
	CPPUNIT_ASSERT (string_to_bool ("N", val));
	CPPUNIT_ASSERT_EQUAL (val, false);

	val = false;
	CPPUNIT_ASSERT (string_to_bool ("y", val));
	CPPUNIT_ASSERT_EQUAL (val, true);

	val = true;
	CPPUNIT_ASSERT (string_to_bool ("n", val));
	CPPUNIT_ASSERT_EQUAL (val, false);

	// test some junk
	CPPUNIT_ASSERT (!string_to_bool ("01234someYNtrueyesno junk0123", val));

	// test the string_to/to_string templates
	CPPUNIT_ASSERT_EQUAL (true, string_to<bool>(to_string (true)));

	CPPUNIT_ASSERT_EQUAL (false, string_to<bool>(to_string (false)));
}

namespace {

bool
check_int_convert ()
{
	int32_t num = g_random_int ();
	return (num == string_to<int32_t>(to_string (num)));
}

bool
check_float_convert ()
{
	float num = (float)g_random_double ();
	return (num == string_to<float>(to_string<float> (num)));
}

bool
check_double_convert ()
{
	double num = g_random_double ();
	return (num == string_to<double>(to_string<double> (num)));
}

static const int s_iter_count = 1000000;

void*
check_int_convert_thread(void*)
{
	for (int n = 0; n < s_iter_count; n++) {
		assert (check_int_convert ());
	}
	return NULL;
}

void*
check_float_convert_thread(void*)
{
	for (int n = 0; n < s_iter_count; n++) {
		assert (check_float_convert ());
	}
	return NULL;
}

void*
check_double_convert_thread(void*)
{
	for (int n = 0; n < s_iter_count; n++) {
		assert (check_double_convert ());
	}
	return NULL;
}

static const double s_test_double = 31459.265359;

void*
check_decimal_mark_is_comma_thread (void*)
{
	for (int n = 0; n < s_iter_count; n++) {
		assert (check_decimal_mark_is_comma ());
	}

	return NULL;
}

} // anon namespace

// Perform the test in the French locale as the format for decimals is
// different and a comma is used as a decimal point. Test that this has no
// impact on the string conversions which are expected to be the same as in the
// C locale.
void
StringConvertTest::test_convert_thread_safety ()
{
	std::string locale_str;

	CPPUNIT_ASSERT(get_locale_with_comma_decimal_mark(locale_str));

	LocaleGuard guard (locale_str);

	CPPUNIT_ASSERT (check_int_convert ());
	CPPUNIT_ASSERT (check_float_convert ());
	CPPUNIT_ASSERT (check_double_convert ());
	CPPUNIT_ASSERT (check_decimal_mark_is_comma ());

	pthread_t convert_int_thread;
	pthread_t convert_float_thread;
	pthread_t convert_double_thread;
	pthread_t fr_printf_thread;

	CPPUNIT_ASSERT (
	    pthread_create (
	        &convert_int_thread, NULL, check_int_convert_thread, NULL) == 0);
	CPPUNIT_ASSERT (
	    pthread_create (
	        &convert_float_thread, NULL, check_float_convert_thread, NULL) == 0);
	CPPUNIT_ASSERT (
	    pthread_create (
	        &convert_double_thread, NULL, check_double_convert_thread, NULL) == 0);
	CPPUNIT_ASSERT (
	    pthread_create (&fr_printf_thread, NULL, check_decimal_mark_is_comma_thread, NULL) ==
	    0);

	void* return_value;

	CPPUNIT_ASSERT (pthread_join (convert_int_thread, &return_value) == 0);
	CPPUNIT_ASSERT (pthread_join (convert_float_thread, &return_value) == 0);
	CPPUNIT_ASSERT (pthread_join (convert_double_thread, &return_value) == 0);
	CPPUNIT_ASSERT (pthread_join (fr_printf_thread, &return_value) == 0);
}