/other_authors/netcdf/snctools/tests/test_nc_datatype_string.m

http://m--pack.googlecode.com/ · MATLAB · 228 lines · 99 code · 100 blank · 29 comment · 11 complexity · d4b86c4534b816d2621e8776020d1896 MD5 · raw file

  1. function test_nc_datatype_string()
  2. % TEST_NC_DATATYPE_STRING:
  3. %
  4. % Bad input argument tests.
  5. % Test 1: no inputs
  6. % Test 2: two inputs
  7. % test 3: input not numeric
  8. % test 4: input is outside of 0-6
  9. %
  10. % These tests should succeed
  11. % test 5: input is 0 ==> 'NC_NAT'
  12. % test 6: input is 1 ==> 'NC_BYTE'
  13. % test 7: input is 2 ==> 'NC_CHAR'
  14. % test 8: input is 3 ==> 'NC_SHORT'
  15. % test 9: input is 4 ==> 'NC_INT'
  16. % test 10: input is 5 ==> 'NC_FLOAT'
  17. % test 11: input is 6 ==> 'NC_DOUBLE'
  18. fprintf ( 1, '%s: starting...\n', upper(mfilename) );
  19. % Test 1: no inputs
  20. testid = 'Test 1';
  21. fprintf ( 1, '%s.\n', testid );
  22. try
  23. dt = nc_datatype_string;
  24. msg = sprintf ( '%s: %s succeeded when it should have failed.\n', mfilename, testid );
  25. error ( msg );
  26. catch
  27. ;
  28. end
  29. fprintf ( 1, '%s: passed.\n', testid );
  30. % Test 2: two inputs
  31. testid = 'Test 2';
  32. fprintf ( 1, '%s.\n', testid );
  33. try
  34. dt = nc_datatype_string ( 0, 1 );
  35. msg = sprintf ( '%s: %s succeeded when it should have failed.\n', mfilename, testid );
  36. error ( msg );
  37. catch
  38. ;
  39. end
  40. fprintf ( 1, '%s: passed.\n', testid );
  41. % test 3: input not numeric
  42. testid = 'Test 3';
  43. fprintf ( 1, '%s.\n', testid );
  44. try
  45. dt = nc_datatype_string ( 'a' );
  46. msg = sprintf ( '%s: %s succeeded when it should have failed.\n', mfilename, testid );
  47. error ( msg );
  48. catch
  49. ;
  50. end
  51. fprintf ( 1, '%s: passed.\n', testid );
  52. % test 4: input is outside of 0-6
  53. testid = 'Test 4';
  54. fprintf ( 1, '%s.\n', testid );
  55. try
  56. dt = nc_datatype_string ( -1 );
  57. msg = sprintf ( '%s: %s succeeded when it should have failed.\n', mfilename, testid );
  58. error ( msg );
  59. catch
  60. ;
  61. end
  62. fprintf ( 1, '%s: passed.\n', testid );
  63. %
  64. % These tests should succeed
  65. % test 5: input is 0 ==> 'NC_NAT'
  66. testid = 'Test 5';
  67. fprintf ( 1, '%s.\n', testid );
  68. dt = nc_datatype_string ( 0 );
  69. if ~strcmp(dt,'NC_NAT')
  70. msg = sprintf ( '%s: %s: failed to convert 0.\n', mfilename, testid );
  71. error ( msg );
  72. end
  73. fprintf ( 1, '%s: passed.\n', testid );
  74. % test 6: input is 1 ==> 'NC_BYTE'
  75. testid = 'Test 6';
  76. fprintf ( 1, '%s.\n', testid );
  77. dt = nc_datatype_string ( 1 );
  78. if ~strcmp(dt,'NC_BYTE')
  79. msg = sprintf ( '%s: %s: failed to convert 1.\n', mfilename, testid );
  80. error ( msg );
  81. end
  82. fprintf ( 1, '%s: passed.\n', testid );
  83. % test 7: input is 2 ==> 'NC_CHAR'
  84. testid = 'Test 7';
  85. fprintf ( 1, '%s.\n', testid );
  86. dt = nc_datatype_string ( 2 );
  87. if ~strcmp(dt,'NC_CHAR')
  88. msg = sprintf ( '%s: %s: failed to convert 2.\n', mfilename, testid );
  89. error ( msg );
  90. end
  91. fprintf ( 1, '%s: passed.\n', testid );
  92. % test 8: input is 3 ==> 'NC_SHORT'
  93. testid = 'Test 8';
  94. fprintf ( 1, '%s.\n', testid );
  95. dt = nc_datatype_string ( 3 );
  96. if ~strcmp(dt,'NC_SHORT')
  97. msg = sprintf ( '%s: %s: failed to convert 3.\n', mfilename, testid );
  98. error ( msg );
  99. end
  100. fprintf ( 1, '%s: passed.\n', testid );
  101. % test 9: input is 4 ==> 'NC_INT'
  102. testid = 'Test 9';
  103. fprintf ( 1, '%s.\n', testid );
  104. dt = nc_datatype_string ( 4 );
  105. if ~strcmp(dt,'NC_INT')
  106. msg = sprintf ( '%s: %s: failed to convert 4.\n', mfilename, testid );
  107. error ( msg );
  108. end
  109. fprintf ( 1, '%s: passed.\n', testid );
  110. % test 10: input is 5 ==> 'NC_FLOAT'
  111. testid = 'Test 10';
  112. fprintf ( 1, '%s.\n', testid );
  113. dt = nc_datatype_string ( 5 );
  114. if ~strcmp(dt,'NC_FLOAT')
  115. msg = sprintf ( '%s: %s: failed to convert 5.\n', mfilename, testid );
  116. error ( msg );
  117. end
  118. fprintf ( 1, '%s: passed.\n', testid );
  119. % test 11: input is 6 ==> 'NC_DOUBLE'
  120. testid = 'Test 11';
  121. fprintf ( 1, '%s.\n', testid );
  122. dt = nc_datatype_string ( 6 );
  123. if ~strcmp(dt,'NC_DOUBLE')
  124. msg = sprintf ( '%s: %s: failed to convert 6.\n', mfilename, testid );
  125. error ( msg );
  126. end
  127. fprintf ( 1, '%s: passed.\n', testid );
  128. fprintf ( 1, '%s: all tests passed.\n', upper(mfilename) );