@@ -64,24 +64,27 @@ public void doubleChunkedArrayValuesOK() {
64
64
final int max_i = 10 ;
65
65
final long chunk_size = 3 ;
66
66
final doubleChunkedArray chunkedArray = new doubleChunkedArray (chunk_size );
67
+ try {
68
+ for (int i = 1 ; i <= max_i ; ++i ) {
69
+ chunkedArray .add (i * 1.1 );
70
+ }
67
71
68
- for (int i = 1 ; i <= max_i ; ++i ) {
69
- chunkedArray .add (i * 1.1 );
70
- }
71
-
72
- int chunk = 0 ;
73
- int pos = 0 ;
74
- for (int i = 0 ; i < max_i ; ++i ) {
75
- final double ref_value = (i +1 ) * 1.1 ;
76
- assertThat (chunkedArray .getitem (chunk , pos , -1 ))
77
- .as ("Value at chunk %d, position %d" , chunk , pos )
78
- .isCloseTo (ref_value , Offset .offset (1e-3 ));
79
-
80
- ++pos ;
81
- if (pos == chunk_size ) {
82
- ++chunk ;
83
- pos = 0 ;
72
+ int chunk = 0 ;
73
+ int pos = 0 ;
74
+ for (int i = 0 ; i < max_i ; ++i ) {
75
+ final double ref_value = (i + 1 ) * 1.1 ;
76
+ assertThat (chunkedArray .getitem (chunk , pos , -1 ))
77
+ .as ("Value at chunk %d, position %d" , chunk , pos )
78
+ .isCloseTo (ref_value , Offset .offset (1e-3 ));
79
+
80
+ ++pos ;
81
+ if (pos == chunk_size ) {
82
+ ++chunk ;
83
+ pos = 0 ;
84
+ }
84
85
}
86
+ } finally {
87
+ chunkedArray .delete ();
85
88
}
86
89
}
87
90
@@ -94,14 +97,18 @@ public void doubleChunkedArrayOutOfBoundsError() {
94
97
final double on_fail_sentinel_value = -1 ;
95
98
final doubleChunkedArray chunkedArray = new doubleChunkedArray (3 );
96
99
97
- // Test out of bounds chunk (only 1 exists, not 11):
98
- assertThat (chunkedArray .getitem (10 , 0 , on_fail_sentinel_value ))
99
- .as ("out-of-bounds return sentinel value" )
100
- .isCloseTo (on_fail_sentinel_value , Offset .offset (1e-3 ));
101
- // Test out of bounds on first chunk:
102
- assertThat (chunkedArray .getitem (0 , 10 , on_fail_sentinel_value ))
103
- .as ("out-of-bounds return sentinel value" )
104
- .isCloseTo (on_fail_sentinel_value , Offset .offset (1e-3 ));
100
+ try {
101
+ // Test out of bounds chunk (only 1 exists, not 11):
102
+ assertThat (chunkedArray .getitem (10 , 0 , on_fail_sentinel_value ))
103
+ .as ("out-of-bounds return sentinel value" )
104
+ .isCloseTo (on_fail_sentinel_value , Offset .offset (1e-3 ));
105
+ // Test out of bounds on first chunk:
106
+ assertThat (chunkedArray .getitem (0 , 10 , on_fail_sentinel_value ))
107
+ .as ("out-of-bounds return sentinel value" )
108
+ .isCloseTo (on_fail_sentinel_value , Offset .offset (1e-3 ));
109
+ } finally {
110
+ chunkedArray .delete ();
111
+ }
105
112
}
106
113
107
114
/**
@@ -112,19 +119,27 @@ public void doubleChunkedArrayOutOfBoundsError() {
112
119
@ Test
113
120
public void ChunkedArrayCoalesceTo () {
114
121
final int numFeatures = 3 ;
115
- final int chunkSize = 2 * numFeatures ; // Must be multiple
122
+ final int chunkSize = 2 * numFeatures ; // Must be multiple
116
123
final doubleChunkedArray chunkedArray = new doubleChunkedArray (chunkSize );
117
- // Fill 1 chunk + some part of other
118
- for (int i = 0 ; i < chunkSize + 1 ; ++i ) {
119
- chunkedArray .add (i );
120
- }
121
- final SWIGTYPE_p_double swigArr = lightgbmlib .new_doubleArray (chunkedArray .get_add_count ());
124
+ try {
125
+ // Fill 1 chunk + some part of other
126
+ for (int i = 0 ; i < chunkSize + 1 ; ++i ) {
127
+ chunkedArray .add (i );
128
+ }
129
+ final SWIGTYPE_p_double swigArr = lightgbmlib .new_doubleArray (chunkedArray .get_add_count ());
130
+ try {
122
131
123
- chunkedArray .coalesce_to (swigArr );
132
+ chunkedArray .coalesce_to (swigArr );
124
133
125
- for (int i = 0 ; i < chunkedArray .get_add_count (); ++i ) {
126
- double v = lightgbmlib .doubleArray_getitem (swigArr , i );
127
- assertThat (v ).as ("coalescedArray[%d]" , i ).isCloseTo (i , Offset .offset (1e-3 ));
134
+ for (int i = 0 ; i < chunkedArray .get_add_count (); ++i ) {
135
+ double v = lightgbmlib .doubleArray_getitem (swigArr , i );
136
+ assertThat (v ).as ("coalescedArray[%d]" , i ).isCloseTo (i , Offset .offset (1e-3 ));
137
+ }
138
+ } finally {
139
+ lightgbmlib .delete_doubleArray (swigArr );
140
+ }
141
+ } finally {
142
+ chunkedArray .delete ();
128
143
}
129
144
}
130
145
@@ -135,35 +150,46 @@ public void ChunkedArrayCoalesceTo() {
135
150
@ Test
136
151
public void LGBM_DatasetCreateFromMatsFromChunkedArray () {
137
152
final int numFeatures = 3 ;
138
- final int chunkSize = 2 * numFeatures ; // Must be multiple
153
+ final int chunkSize = 2 * numFeatures ; // Must be multiple
139
154
final doubleChunkedArray chunkedArray = new doubleChunkedArray (chunkSize );
140
- // Fill 1 chunk + some part of other
141
- for (int i = 0 ; i < chunkSize + 1 ; ++i ) {
142
- chunkedArray .add (i );
143
- }
155
+ try {
156
+ // Fill 1 chunk + some part of other
157
+ for (int i = 0 ; i < chunkSize + 1 ; ++i ) {
158
+ chunkedArray .add (i );
159
+ }
144
160
145
- final long numChunks = chunkedArray .get_chunks_count ();
146
- SWIGTYPE_p_int chunkSizes = lightgbmlib .new_intArray (numChunks );
147
- for (int i = 0 ; i < numChunks - 1 ; ++i ) {
148
- lightgbmlib .intArray_setitem (chunkSizes , i , chunkSize );
161
+ final long numChunks = chunkedArray .get_chunks_count ();
162
+ final SWIGTYPE_p_int chunkSizes = lightgbmlib .new_intArray (numChunks );
163
+ try {
164
+ for (int i = 0 ; i < numChunks - 1 ; ++i ) {
165
+ lightgbmlib .intArray_setitem (chunkSizes , i , chunkSize );
166
+ }
167
+ lightgbmlib .intArray_setitem (chunkSizes , numChunks - 1 , (int ) chunkedArray .get_current_chunk_added_count ());
168
+
169
+ final SWIGTYPE_p_p_void swigOutDatasetHandlePtr = lightgbmlib .voidpp_handle ();
170
+ try {
171
+ final int returnCodeLGBM = lightgbmlib .LGBM_DatasetCreateFromMats (
172
+ (int ) chunkedArray .get_chunks_count (),
173
+ chunkedArray .data_as_void (),
174
+ lightgbmlibConstants .C_API_DTYPE_FLOAT64 ,
175
+ chunkSizes ,
176
+ numFeatures ,
177
+ 1 ,
178
+ "" , // parameters
179
+ null ,
180
+ swigOutDatasetHandlePtr
181
+ );
182
+
183
+ assertThat (returnCodeLGBM ).as ("LightGBM return code" ).isEqualTo (0 );
184
+ } finally {
185
+ lightgbmlib .delete_voidpp (swigOutDatasetHandlePtr );
186
+ }
187
+ } finally {
188
+ lightgbmlib .delete_intArray (chunkSizes );
189
+ }
190
+ } finally {
191
+ chunkedArray .delete ();
149
192
}
150
- lightgbmlib .intArray_setitem (chunkSizes , numChunks -1 , (int )chunkedArray .get_current_chunk_added_count ());
151
-
152
- final SWIGTYPE_p_p_void swigOutDatasetHandlePtr = lightgbmlib .voidpp_handle ();;
153
-
154
- final int returnCodeLGBM = lightgbmlib .LGBM_DatasetCreateFromMats (
155
- (int )chunkedArray .get_chunks_count (),
156
- chunkedArray .data_as_void (),
157
- lightgbmlibConstants .C_API_DTYPE_FLOAT64 ,
158
- chunkSizes ,
159
- numFeatures ,
160
- 1 ,
161
- "" , // parameters
162
- null ,
163
- swigOutDatasetHandlePtr
164
- );
165
-
166
- assertThat (returnCodeLGBM ).as ("LightGBM return code" ).isEqualTo (0 );
167
193
}
168
194
169
195
}
0 commit comments