Commit 88013475334ef3d8dcf8604c3b3af4867fa94aef

Authored by Jürgen Knödlseder
1 parent 44cf8169

Add validity check for remaining FITS table columns and add Python unit test.

pyext/GFitsTableBitCol.i
... ... @@ -71,16 +71,44 @@ public:
71 71 ***************************************************************************/
72 72 %extend GFitsTableBitCol {
73 73 bool __getitem__(int GFitsTableColInx[]) {
74   - if (GFitsTableColInx[0] == 1)
  74 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  75 + throw GException::out_of_range("__getitem__()", "Row index",
  76 + GFitsTableColInx[1], self->length());
  77 + }
  78 + if (GFitsTableColInx[0] == 1) {
75 79 return (*self)(GFitsTableColInx[1]);
76   - else
77   - return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  80 + }
  81 + else {
  82 + if (GFitsTableColInx[2] >= 0 &&
  83 + GFitsTableColInx[2] < self->elements(GFitsTableColInx[1])) {
  84 + return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  85 + }
  86 + else {
  87 + throw GException::out_of_range("__getitem__()", "Column index",
  88 + GFitsTableColInx[2],
  89 + self->elements(GFitsTableColInx[1]));
  90 + }
  91 + }
78 92 }
79 93 void __setitem__(int GFitsTableColInx[], bool value) {
80   - if (GFitsTableColInx[0] == 1)
  94 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  95 + throw GException::out_of_range("__setitem__()", "Row index",
  96 + GFitsTableColInx[1], self->length());
  97 + }
  98 + if (GFitsTableColInx[0] == 1) {
81 99 (*self)(GFitsTableColInx[1]) = value;
82   - else
83   - (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  100 + }
  101 + else {
  102 + if (GFitsTableColInx[2] >= 0 && GFitsTableColInx[2] <
  103 + self->elements(GFitsTableColInx[1])) {
  104 + (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  105 + }
  106 + else {
  107 + throw GException::out_of_range("__setitem__()", "Column index",
  108 + GFitsTableColInx[2],
  109 + self->elements(GFitsTableColInx[1]));
  110 + }
  111 + }
84 112 }
85 113 GFitsTableBitCol copy() {
86 114 return (*self);
... ...
pyext/GFitsTableBoolCol.i
... ... @@ -71,16 +71,44 @@ public:
71 71 ***************************************************************************/
72 72 %extend GFitsTableBoolCol {
73 73 bool __getitem__(int GFitsTableColInx[]) {
74   - if (GFitsTableColInx[0] == 1)
  74 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  75 + throw GException::out_of_range("__getitem__()", "Row index",
  76 + GFitsTableColInx[1], self->length());
  77 + }
  78 + if (GFitsTableColInx[0] == 1) {
75 79 return (*self)(GFitsTableColInx[1]);
76   - else
77   - return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  80 + }
  81 + else {
  82 + if (GFitsTableColInx[2] >= 0 &&
  83 + GFitsTableColInx[2] < self->elements(GFitsTableColInx[1])) {
  84 + return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  85 + }
  86 + else {
  87 + throw GException::out_of_range("__getitem__()", "Column index",
  88 + GFitsTableColInx[2],
  89 + self->elements(GFitsTableColInx[1]));
  90 + }
  91 + }
78 92 }
79 93 void __setitem__(int GFitsTableColInx[], bool value) {
80   - if (GFitsTableColInx[0] == 1)
  94 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  95 + throw GException::out_of_range("__setitem__()", "Row index",
  96 + GFitsTableColInx[1], self->length());
  97 + }
  98 + if (GFitsTableColInx[0] == 1) {
81 99 (*self)(GFitsTableColInx[1]) = value;
82   - else
83   - (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  100 + }
  101 + else {
  102 + if (GFitsTableColInx[2] >= 0 && GFitsTableColInx[2] <
  103 + self->elements(GFitsTableColInx[1])) {
  104 + (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  105 + }
  106 + else {
  107 + throw GException::out_of_range("__setitem__()", "Column index",
  108 + GFitsTableColInx[2],
  109 + self->elements(GFitsTableColInx[1]));
  110 + }
  111 + }
84 112 }
85 113 GFitsTableBoolCol copy() {
86 114 return (*self);
... ...
pyext/GFitsTableByteCol.i
... ... @@ -70,16 +70,44 @@ public:
70 70 ***************************************************************************/
71 71 %extend GFitsTableByteCol {
72 72 unsigned char __getitem__(int GFitsTableColInx[]) {
73   - if (GFitsTableColInx[0] == 1)
  73 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  74 + throw GException::out_of_range("__getitem__()", "Row index",
  75 + GFitsTableColInx[1], self->length());
  76 + }
  77 + if (GFitsTableColInx[0] == 1) {
74 78 return (*self)(GFitsTableColInx[1]);
75   - else
76   - return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  79 + }
  80 + else {
  81 + if (GFitsTableColInx[2] >= 0 &&
  82 + GFitsTableColInx[2] < self->elements(GFitsTableColInx[1])) {
  83 + return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  84 + }
  85 + else {
  86 + throw GException::out_of_range("__getitem__()", "Column index",
  87 + GFitsTableColInx[2],
  88 + self->elements(GFitsTableColInx[1]));
  89 + }
  90 + }
77 91 }
78 92 void __setitem__(int GFitsTableColInx[], unsigned char value) {
79   - if (GFitsTableColInx[0] == 1)
  93 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  94 + throw GException::out_of_range("__setitem__()", "Row index",
  95 + GFitsTableColInx[1], self->length());
  96 + }
  97 + if (GFitsTableColInx[0] == 1) {
80 98 (*self)(GFitsTableColInx[1]) = value;
81   - else
82   - (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  99 + }
  100 + else {
  101 + if (GFitsTableColInx[2] >= 0 && GFitsTableColInx[2] <
  102 + self->elements(GFitsTableColInx[1])) {
  103 + (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  104 + }
  105 + else {
  106 + throw GException::out_of_range("__setitem__()", "Column index",
  107 + GFitsTableColInx[2],
  108 + self->elements(GFitsTableColInx[1]));
  109 + }
  110 + }
83 111 }
84 112 GFitsTableByteCol copy() {
85 113 return (*self);
... ...
pyext/GFitsTableCDoubleCol.i
... ... @@ -75,17 +75,45 @@ public:
75 75 %extend GFitsTableCDoubleCol {
76 76 /*
77 77 GFits::cdouble __getitem__(int GFitsTableColInx[]) {
78   - if (GFitsTableColInx[0] == 1)
  78 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  79 + throw GException::out_of_range("__getitem__()", "Row index",
  80 + GFitsTableColInx[1], self->length());
  81 + }
  82 + if (GFitsTableColInx[0] == 1) {
79 83 return (*self)(GFitsTableColInx[1]);
80   - else
81   - return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  84 + }
  85 + else {
  86 + if (GFitsTableColInx[2] >= 0 &&
  87 + GFitsTableColInx[2] < self->elements(GFitsTableColInx[1])) {
  88 + return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  89 + }
  90 + else {
  91 + throw GException::out_of_range("__getitem__()", "Column index",
  92 + GFitsTableColInx[2],
  93 + self->elements(GFitsTableColInx[1]));
  94 + }
  95 + }
82 96 }
83 97 */
84 98 void __setitem__(int GFitsTableColInx[], GFits::cdouble value) {
85   - if (GFitsTableColInx[0] == 1)
  99 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  100 + throw GException::out_of_range("__setitem__()", "Row index",
  101 + GFitsTableColInx[1], self->length());
  102 + }
  103 + if (GFitsTableColInx[0] == 1) {
86 104 (*self)(GFitsTableColInx[1]) = value;
87   - else
88   - (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  105 + }
  106 + else {
  107 + if (GFitsTableColInx[2] >= 0 && GFitsTableColInx[2] <
  108 + self->elements(GFitsTableColInx[1])) {
  109 + (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  110 + }
  111 + else {
  112 + throw GException::out_of_range("__setitem__()", "Column index",
  113 + GFitsTableColInx[2],
  114 + self->elements(GFitsTableColInx[1]));
  115 + }
  116 + }
89 117 }
90 118 GFitsTableCDoubleCol copy() {
91 119 return (*self);
... ...
pyext/GFitsTableCFloatCol.i
... ... @@ -75,17 +75,45 @@ public:
75 75 %extend GFitsTableCFloatCol {
76 76 /*
77 77 GFits::cfloat __getitem__(int GFitsTableColInx[]) {
78   - if (GFitsTableColInx[0] == 1)
  78 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  79 + throw GException::out_of_range("__getitem__()", "Row index",
  80 + GFitsTableColInx[1], self->length());
  81 + }
  82 + if (GFitsTableColInx[0] == 1) {
79 83 return (*self)(GFitsTableColInx[1]);
80   - else
81   - return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  84 + }
  85 + else {
  86 + if (GFitsTableColInx[2] >= 0 &&
  87 + GFitsTableColInx[2] < self->elements(GFitsTableColInx[1])) {
  88 + return (*self)(GFitsTableColInx[1], GFitsTableColInx[2]);
  89 + }
  90 + else {
  91 + throw GException::out_of_range("__getitem__()", "Column index",
  92 + GFitsTableColInx[2],
  93 + self->elements(GFitsTableColInx[1]));
  94 + }
  95 + }
82 96 }
83 97 */
84 98 void __setitem__(int GFitsTableColInx[], GFits::cfloat value) {
85   - if (GFitsTableColInx[0] == 1)
  99 + if (GFitsTableColInx[1] < 0 || GFitsTableColInx[1] >= self->length()) {
  100 + throw GException::out_of_range("__setitem__()", "Row index",
  101 + GFitsTableColInx[1], self->length());
  102 + }
  103 + if (GFitsTableColInx[0] == 1) {
86 104 (*self)(GFitsTableColInx[1]) = value;
87   - else
88   - (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  105 + }
  106 + else {
  107 + if (GFitsTableColInx[2] >= 0 && GFitsTableColInx[2] <
  108 + self->elements(GFitsTableColInx[1])) {
  109 + (*self)(GFitsTableColInx[1], GFitsTableColInx[2]) = value;
  110 + }
  111 + else {
  112 + throw GException::out_of_range("__setitem__()", "Column index",
  113 + GFitsTableColInx[2],
  114 + self->elements(GFitsTableColInx[1]));
  115 + }
  116 + }
89 117 }
90 118 GFitsTableCFloatCol copy() {
91 119 return (*self);
... ...
test/test_GFits.py
... ... @@ -48,6 +48,7 @@ class Test(gammalib.GPythonTestSuite):
48 48  
49 49 # Append tests
50 50 self.append(self.test_fits, "Test GFits")
  51 + self.append(self.test_fits_table_columns, "Test FITS table columns")
51 52  
52 53 # Return
53 54 return
... ... @@ -218,3 +219,49 @@ class Test(gammalib.GPythonTestSuite):
218 219  
219 220 # Return
220 221 return
  222 +
  223 + def test_fits_table_columns(self):
  224 + """
  225 + Test FITS table columns
  226 + """
  227 + # Create table columns
  228 + nrows = 10
  229 + col1 = gammalib.GFitsTableBitCol("BIT", nrows)
  230 + col2 = gammalib.GFitsTableBoolCol("BOOLEAN", nrows)
  231 + col3 = gammalib.GFitsTableByteCol("BYTE", nrows)
  232 + col4 = gammalib.GFitsTableDoubleCol("DOUBLE", nrows)
  233 + col5 = gammalib.GFitsTableFloatCol("FLOAT", nrows)
  234 + col6 = gammalib.GFitsTableLongCol("LONG", nrows)
  235 + col7 = gammalib.GFitsTableLongLongCol("LONGLONG", nrows)
  236 + col8 = gammalib.GFitsTableShortCol("SHORT", nrows)
  237 + col9 = gammalib.GFitsTableStringCol("STRING", nrows, 20)
  238 + col10 = gammalib.GFitsTableULongCol("ULONG", nrows)
  239 + col11 = gammalib.GFitsTableUShortCol("USHORT", nrows)
  240 +
  241 + # Test iterators
  242 + for row in col1:
  243 + pass
  244 + for row in col2:
  245 + pass
  246 + for row in col3:
  247 + pass
  248 + for row in col4:
  249 + pass
  250 + for row in col5:
  251 + pass
  252 + for row in col6:
  253 + pass
  254 + for row in col7:
  255 + pass
  256 + for row in col8:
  257 + pass
  258 + for row in col9:
  259 + pass
  260 + for row in col10:
  261 + pass
  262 + for row in col11:
  263 + pass
  264 +
  265 + # Return
  266 + return
  267 +
221 268 \ No newline at end of file
... ...