stringf.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** stringf Procedure Test
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. **
  7. ** This package is free software; you can redistribute it and/or
  8. ** modify it under the terms of the GNU General Public License as
  9. ** published by the Free Software Foundation; either version 2 of
  10. ** the License, or (at your option) any later version.
  11. **
  12. ** This package is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ** General Public License for more details.
  16. **
  17. ** === N O T E S ===========================================================
  18. **
  19. ** ========================================================================= */
  20. #include <iostream>
  21. #include <ETL/stringf>
  22. #include <stdio.h>
  23. using namespace etl;
  24. using namespace std;
  25. int basic_test(void)
  26. {
  27. int ret = 0;
  28. char mystring[80] = "My formatted string!";
  29. string myotherstring = "my other string!";
  30. cout << strprintf("This is a test of >>%s<<.", mystring) << endl;
  31. myotherstring = "5 6.75 George 7";
  32. int i, i2;
  33. float f;
  34. #ifndef ETL_NO_STRSCANF
  35. strscanf(myotherstring, "%d %f %s %d", &i, &f, mystring, &i2);
  36. #else
  37. cout << "warning: strscanf() disabled at compile time..." << endl;
  38. i = 5;
  39. f = 6.75;
  40. i2 = 7;
  41. #endif
  42. cout << myotherstring + "==" + strprintf("%d %f %s %d", i, f, mystring, i2) << endl;
  43. cout << stratof(strprintf("32.5849")) << "==32.5849" << endl;
  44. return ret;
  45. }
  46. int base_and_dir_name_test(void)
  47. {
  48. int ret = 0;
  49. string str(unix_to_local_path("/usr/bin/bleh.exe"));
  50. cout << "Test Case 1 -> " << str << endl;
  51. cout << "basename -> " << basename(str) << endl;
  52. if (basename(str) != "bleh.exe") {
  53. cerr << "error:Bad basename" << endl, ret++;
  54. }
  55. cout << "dirname -> " << dirname(str) << endl;
  56. if (dirname(str) != unix_to_local_path("/usr/bin")) {
  57. cerr << "error:Bad dirname" << endl, ret++;
  58. }
  59. cout << endl;
  60. str = unix_to_local_path("/usr/bin/");
  61. cout << "Test Case 2 -> " << str << endl;
  62. cout << "basename -> " << basename(str) << endl;
  63. if (basename(str) != "bin") {
  64. cerr << "error:Bad basename" << endl, ret++;
  65. }
  66. cout << "dirname -> " << dirname(str) << endl;
  67. if (dirname(str) != unix_to_local_path("/usr")) {
  68. cerr << "error:Bad dirname" << endl, ret++;
  69. }
  70. cout << endl;
  71. str = "bleh.exe";
  72. cout << "Test Case 3 -> " << str << endl;
  73. cout << "basename -> " << basename(str) << endl;
  74. if (basename(str) != "bleh.exe") {
  75. cerr << "error:Bad basename" << endl, ret++;
  76. }
  77. cout << "dirname -> " << dirname(str) << endl;
  78. if (dirname(str) != unix_to_local_path(".")) {
  79. cerr << "error:Bad dirname" << endl, ret++;
  80. }
  81. cout << endl;
  82. return ret;
  83. }
  84. int relative_path_test()
  85. {
  86. int ret = 0;
  87. string curr_path = unix_to_local_path("/usr/local/bin/.");
  88. string dest_path = unix_to_local_path("/usr/share");
  89. cout << "curr_path=" << curr_path << " dest_path=" << dest_path << endl;
  90. cout << "relative_path=" << relative_path(curr_path, dest_path) << endl;
  91. if (relative_path(curr_path, dest_path) != unix_to_local_path("../../share")) {
  92. cerr << "Bad relative path" << endl, ret++;
  93. }
  94. cout << endl;
  95. curr_path = unix_to_local_path("/home/darco/projects/voria");
  96. dest_path = unix_to_local_path("/home/darco/projects/voria/myfile.txt");
  97. cout << "curr_path=" << curr_path << " dest_path=" << dest_path << endl;
  98. cout << "relative_path=" << relative_path(curr_path, dest_path) << endl;
  99. if (relative_path(curr_path, dest_path) != unix_to_local_path("myfile.txt")) {
  100. cerr << "Bad relative path" << endl, ret++;
  101. }
  102. cout << endl;
  103. curr_path = unix_to_local_path("/home/darco/projects/voria");
  104. dest_path = unix_to_local_path("/home/darco/projects/voria/files/myfile.txt");
  105. cout << "curr_path=" << curr_path << " dest_path=" << dest_path << endl;
  106. cout << "relative_path=" << relative_path(curr_path, dest_path) << endl;
  107. if (relative_path(curr_path, dest_path) != unix_to_local_path("files/myfile.txt")) {
  108. cerr << "Bad relative path" << endl, ret++;
  109. }
  110. cout << endl;
  111. curr_path = unix_to_local_path("/usr/local/../include/sys/../linux/linux.h");
  112. cout << "dirty_path=" << curr_path << endl;
  113. cout << "clean_path=" << cleanup_path(curr_path) << endl;
  114. cout << "current_working_directory=" << current_working_directory() << endl;
  115. return ret;
  116. }
  117. int main()
  118. {
  119. int error = 0;
  120. error += basic_test();
  121. error += base_and_dir_name_test();
  122. error += relative_path_test();
  123. return error;
  124. }