Main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EABase/eabase.h>
  5. #include <EAMain/EAMain.h>
  6. #include <EAMain/EAMainInitFini.inl>
  7. #include <EAMain/EAEntryPointMain.inl>
  8. #include <string.h>
  9. #ifdef _MSC_VER
  10. #pragma warning(push, 0)
  11. #endif
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #if defined(EA_COMPILER_MSVC) && defined(EA_PLATFORM_MICROSOFT)
  15. #include <crtdbg.h>
  16. #endif
  17. #ifdef _MSC_VER
  18. #pragma warning(pop)
  19. #endif
  20. void* operator new[](size_t size, const char* /*pName*/, int /*flags*/, unsigned /*debugFlags*/, const char* /*file*/, int /*line*/)
  21. {
  22. return operator new[](size);
  23. }
  24. void* operator new[](size_t size, size_t /*alignment*/, size_t /*alignmentOffset*/, const char* /*pName*/,
  25. int /*flags*/, unsigned /*debugFlags*/, const char* /*file*/, int /*line*/)
  26. {
  27. return operator new[](size);
  28. }
  29. static int TestCommandLineArgs()
  30. {
  31. using namespace EA::EAMain;
  32. #define EAMAIN_TEST(x) if (!(x)) { Report("%s(%d): %s\n", __FILE__, __LINE__, #x); ++nErrorCount; } else (void)0
  33. #define EAMAIN_TEST_FATAL(x) if (!(x)) { Report("%s(%d): %s\n", __FILE__, __LINE__, #x); ++nErrorCount; return nErrorCount; } else (void)0
  34. int nErrorCount = 0;
  35. // This test is disabled on xenon because xenon has implicit command line
  36. // construction if argc is zero.
  37. {
  38. int argc = 0;
  39. char *argv[] = { NULL };
  40. CommandLine commandLine(argc, argv);
  41. EAMAIN_TEST(commandLine.Argc() == 0);
  42. EAMAIN_TEST(commandLine.Argv() != NULL);
  43. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  44. EAMAIN_TEST(commandLine.Arg(0) == nullptr);
  45. EAMAIN_TEST(commandLine.Argv()[0] == nullptr);
  46. }
  47. {
  48. int argc = 1;
  49. char arg[] = "program.elf";
  50. char *argv[] = { arg };
  51. CommandLine commandLine(argc, argv);
  52. EAMAIN_TEST(commandLine.Argc() == 1);
  53. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  54. EAMAIN_TEST(strcmp(commandLine.Argv()[0], "program.elf") == 0);
  55. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  56. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  57. EAMAIN_TEST(commandLine.Arg(1) == nullptr);
  58. EAMAIN_TEST(commandLine.Argv()[1] == nullptr);
  59. }
  60. {
  61. const char *commandLineString = "program.elf";
  62. CommandLine commandLine(commandLineString);
  63. EAMAIN_TEST(commandLine.Argc() == 1);
  64. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  65. EAMAIN_TEST(strcmp(commandLine.Argv()[0], "program.elf") == 0);
  66. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  67. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  68. EAMAIN_TEST(commandLine.Arg(1) == nullptr);
  69. EAMAIN_TEST(commandLine.Argv()[1] == nullptr);
  70. }
  71. {
  72. const char *commandLineString = "program.elf arg1 \"arg 2\"";
  73. CommandLine commandLine(commandLineString);
  74. EAMAIN_TEST(commandLine.Argc() == 3);
  75. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  76. EAMAIN_TEST(strcmp(commandLine.Argv()[0], "program.elf") == 0);
  77. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  78. EAMAIN_TEST(strcmp(commandLine.Arg(1), "arg1") == 0);
  79. EAMAIN_TEST(strcmp(commandLine.Arg(2), "arg 2") == 0);
  80. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  81. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  82. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  83. }
  84. {
  85. const char *commandLineString = "program.elf -x -y:1";
  86. const char *parameter = NULL;
  87. CommandLine commandLine(commandLineString);
  88. EAMAIN_TEST(commandLine.Argc() == 3);
  89. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  90. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  91. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  92. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  93. EAMAIN_TEST(commandLine.FindSwitch("-y", false, &parameter) == 2);
  94. EAMAIN_TEST_FATAL(parameter != NULL);
  95. EAMAIN_TEST(strcmp(parameter, "1") == 0);
  96. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  97. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  98. }
  99. {
  100. const char *commandLineString = "program.elf -x \"-switch:this switch parameter has spaces\"";
  101. const char *parameter = NULL;
  102. CommandLine commandLine(commandLineString);
  103. EAMAIN_TEST(commandLine.Argc() == 3);
  104. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  105. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  106. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  107. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  108. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  109. EAMAIN_TEST_FATAL(parameter != NULL);
  110. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  111. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  112. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  113. }
  114. {
  115. const char *commandLineString = "program.elf -x -switch:\"this switch parameter has spaces\" \"-switch2:as does this one\"";
  116. const char *parameter = NULL;
  117. CommandLine commandLine(commandLineString);
  118. EAMAIN_TEST(commandLine.Argc() == 4);
  119. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  120. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  121. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  122. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  123. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  124. EAMAIN_TEST_FATAL(parameter != NULL);
  125. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  126. EAMAIN_TEST(commandLine.Arg(4) == nullptr);
  127. EAMAIN_TEST(commandLine.Argv()[4] == nullptr);
  128. }
  129. {
  130. const char *commandLineString = "-x -switch:\"this switch parameter has spaces\" \"-switch2:as does this one\"";
  131. const char *parameter = NULL;
  132. CommandLine commandLine(commandLineString, CommandLine::FLAG_NO_PROGRAM_NAME);
  133. EAMAIN_TEST(commandLine.Argc() == 4);
  134. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  135. EAMAIN_TEST(strcmp(commandLine.Arg(0), "") == 0);
  136. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  137. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  138. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  139. EAMAIN_TEST_FATAL(parameter != NULL);
  140. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  141. EAMAIN_TEST(commandLine.Arg(4) == nullptr);
  142. EAMAIN_TEST(commandLine.Argv()[4] == nullptr);
  143. }
  144. {
  145. int argc = 3;
  146. char arg0[] = "program.elf";
  147. char arg1[] = "-x";
  148. char arg2[] = "-switch:this switch parameter has spaces";
  149. char *argv[] = { arg0, arg1, arg2 };
  150. const char *parameter;
  151. CommandLine commandLine(argc, argv);
  152. EAMAIN_TEST(commandLine.Argc() == 3);
  153. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  154. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  155. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  156. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  157. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  158. EAMAIN_TEST_FATAL(parameter != NULL);
  159. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  160. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  161. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  162. }
  163. return nErrorCount;
  164. #undef EAMAIN_TEST
  165. #undef EAMAIN_TEST_FATAL
  166. }
  167. static bool gEAMainInitCalled;
  168. void EAMainInit()
  169. {
  170. gEAMainInitCalled = true;
  171. }
  172. void EAMainFini()
  173. {
  174. }
  175. ///////////////////////////////////////////////////////////////////////////////
  176. // EAMain
  177. //
  178. int EAMain(int argc, char** argv)
  179. {
  180. using namespace EA::EAMain;
  181. int nErrorCount(0);
  182. Report("List of arguments passed:\n");
  183. for (int i = 0; i < argc; ++i)
  184. {
  185. Report("Arg %d: %s\n", i, argv[i]);
  186. }
  187. Report("\n");
  188. // Basic test of redirection of stdout/stderr on platforms that support
  189. // redirection of these streams.
  190. printf("printf(...)\n");
  191. fprintf(stdout, "fprintf(stdout, ...)\n");
  192. fflush(stdout);
  193. fprintf(stderr, "fprintf(stderr, ...)\n");
  194. fflush(stderr);
  195. Report("Test of %s.\n", "Report()");
  196. Report("Report()\n");
  197. bool bArgPassed = false;
  198. for (int i = 0; i < argc; ++i)
  199. {
  200. if (strcmp(argv[i], "-testargpassing") == 0)
  201. {
  202. bArgPassed = true;
  203. }
  204. }
  205. // Windows Phone does not support passing arguments to app bundles when
  206. // launching them.
  207. #if !defined(EA_PLATFORM_WINDOWS_PHONE)
  208. if (!bArgPassed)
  209. {
  210. Report("Arg not passed!\n");
  211. ++nErrorCount;
  212. }
  213. #endif
  214. if (TestCommandLineArgs() != 0)
  215. {
  216. Report("Error parsing command line arguments!\n");
  217. ++nErrorCount;
  218. }
  219. if (!gEAMainInitCalled)
  220. {
  221. Report("EAMainInit was not called!\n");
  222. ++nErrorCount;
  223. }
  224. Report("This report statement has \nno terminating newline");
  225. return nErrorCount;
  226. }