daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Jan 18th, 2018 102 in 28 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use v6;
  2. use Test; plan 34;
  3. use Data::StaticTable;
  4.  
  5. #===============================================================================
  6. diag "== Basic tests ==";
  7.  
  8. dies-ok
  9. { my $ = Data::StaticTable.new(<a a b>, (1 .. 6)) },
  10. "Can not create Data::StaticTables with repeated header names";
  11.  
  12. dies-ok
  13. { my $ = Data::StaticTable.new((), (1 .. 6)) },
  14. "Can not create Data::StaticTables with an empty header";
  15.  
  16. my $t1 = Data::StaticTable.new(
  17.     10,       # 10 nameless columns. Named automatically as 'A', 'B', etc.
  18.     (1 .. 10) # 10 cells with data, one row
  19. );
  20. diag $t1.display;
  21. ok($t1.header ~~ <A B C D E F G H I J>, "Column names are automatically filled");
  22. ok($t1.column{'A'} == 1, "Column 1 is automatically labeled 'A'");
  23. ok($t1.column{'E'} == 5, "Column 5 is automatically labeled 'E'");
  24. ok($t1.rows == 1, "StaticTable has one row only");
  25. ok($t1.elems == 10, "StaticTable contains 10 elements");
  26. #===============================================================================
  27. diag "== Adding empty cells when data is not enough ==";
  28. my $t2 = Data::StaticTable.new(
  29.     10,       # 10 nameless columns. Named automatically as 'A', 'B', etc.
  30.     (1 .. 11) # More than 10 elements. Creates a row almost empty
  31. );
  32. diag $t2.display;
  33. ok($t2.columns == 10, "Data::StaticTable t2 has 10 columns");
  34. ok($t2.rows == 2, "Data::StaticTable t2 has 2 rows");
  35. ok($t2.get-cell('C', 2) ~~ Any, "Filler cell in 'C',2 is Any element");
  36. ok($t2.elems == 20, "StaticTable contains 20 elements (11 + extra 9)");
  37.  
  38. #===============================================================================
  39. diag "== Referencing individual cells ==";
  40. my $t3 = Data::StaticTable.new(
  41.     <Col1 Col2 Col3>,
  42.     (
  43.     1, 2, 3,               # Row 1
  44.     "four", "five", "six", # Row 2
  45.     Any, Nil, "NINE"       # Row 3
  46.     )
  47. );
  48. diag $t3.display;
  49. ok($t3.get-cell('Col3',1) == 3, "Reading a cell directly");
  50. ok($t3.get-cell('Col1',3).defined == False, "Asking for undefined cell assigned Any");
  51. ok($t3.get-cell('Col2',3).defined == False, "Asking for undefined cell assigned Nil");
  52. ok($t3.get-cell('Col3',3).defined == True, "Asking for defined cell");
  53. dies-ok
  54. { $t3.get-cell('Col999', 2) },
  55. "Can not access with an inexistant column";
  56. dies-ok
  57. { $t3.get-cell('Col1', 4) },
  58. "Can not access with an inexistant row";
  59.  
  60.  
  61.  
  62. #===============================================================================
  63. diag "== Reading cells, rows and columns ==";
  64. my $t4 = Data::StaticTable.new(
  65.   <A  B   C>,  # Header with 3 columns and 66 elements of data
  66.   ( #Columns
  67.   1,  2,  3,  #Row 1
  68.   4,  5,  6,  #Row 2
  69.   7,  8,  9,  #Row 3
  70.   10, 11, 12, #Row 4
  71.   13, 14, 15, #Row 5
  72.   16, 17, 18, #Row 6
  73.   19, 20, 21, #Row 7
  74.   22, 23, 24, #Row 8
  75.   25, 26, 27, #Row 9
  76.   28, 29, 30, #Row 10
  77.   31, 32, 33, #Row 11
  78.   34, 35, 36, #Row 12
  79.   37, 38, 39, #Row 13
  80.   40, 41, 42, #Row 14
  81.   43, 44, 45, #Row 15
  82.   46, 47, 48, #Row 16
  83.   49, 50, 51, #Row 17
  84.   52, 53, 54, #Row 18
  85.   55, 56, 57, #Row 19
  86.   58, 59, 60, #Row 20
  87.   61, 62, 63, #Row 21
  88.   64, 65, 66) #Row 22
  89. );
  90. diag $t4.display;
  91. ok($t4.header ~~ <A B C>, "StaticTable has the correct header");
  92. ok($t4.columns == 3, "StaticTable has 3 columns");
  93. ok($t4.column{'B'} == 2, "StaticTable second column has the header name 'B'");
  94. ok($t4.column{'Z'} ~~ Any, "StaticTable has no column with the header name 'Z'");
  95. ok($t4.rows == 22, "StaticTable has 22 rows");
  96. ok($t4.get-column("A") ~~ (1, 4 ... 64).list, "Column has the correct data in the 'A' column");
  97. ok($t4.get-cell('C', 1) == 3, "Correct value found in t3 at 'C',1");
  98. ok(35 eq $t4.get-cell('B', 12), "Correct value found at 'B',12");
  99. ok((4,5,6) ~~ $t4.get-row(2) , "Correct row number 2 found");
  100. ok($t4.header.elems == 3, "Header has 3 elements");
  101.  
  102. diag "== Complex cells and shaped arrays ==";
  103. my $t10 = Data::StaticTable.new(
  104.      <Attr          Dim1   Dim2    Dim3    Dim4>, #--   <- Header
  105.     (
  106.     'attribute-1',  1,      2,      3,      'D+', # Row 1
  107.     'attribute-2',  4,      5,      6,      'B+', # Row 2
  108.     'attribute-3',  7,      8,      9,      'A-', # Row 3
  109.     'attribute-4', ('ALPHA',                      # \\\\\
  110.                     'BETA',                       # Row 4
  111.                      3.0),  5,      6,      'A++',# \\\\\
  112.     'attribute-10', 0,      0,      0,      'Z',  # Row 5
  113.     'attribute-11', (-2 .. 2), Nil, Nil,    'X'   # Row 6
  114.     )
  115. );
  116. diag $t10.display;
  117. ok($t10.columns == 5, "StaticTable has 5 columns");
  118. ok($t10.header.elems == 5, "StaticTable has 5 elements in header");
  119. ok($t10.rows == 6, "StaticTable has 6 rows");
  120.  
  121. my @shape = $t10.get-shaped-array();
  122. ok (@shape[3;1;0] eq 'ALPHA', "Shape spec works as expected (3 dimensions)");
  123. ok (@shape[3;1;1] eq 'BETA', "Shape spec works as expected (3 dimensions)");
  124. ok (@shape[3;1;2] == 3, "Shape spec works as expected (3 dimensions)");
  125. ok (@shape[1;2] == 7, "Shape spec works as expected (2 dimensions)");
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top