Skip to content

Commit 76cb6a9

Browse files
feat: Add create command of cobj-idx (#648)
1 parent a955ff2 commit 76cb6a9

File tree

8 files changed

+651
-69
lines changed

8 files changed

+651
-69
lines changed

libcobj/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ SRC_FILES = \
6969
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/ui/CobolResultString.java \
7070
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/ui/CobolResultDouble.java \
7171
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/IndexedFileUtilMain.java \
72+
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/CobolFileKeyInfo.java \
7273
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/ErrorLib.java \
7374
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/LoadResult.java \
7475
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/UserDataFormat.java \

libcobj/Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ SRC_FILES = \
346346
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/ui/CobolResultString.java \
347347
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/ui/CobolResultDouble.java \
348348
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/IndexedFileUtilMain.java \
349+
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/CobolFileKeyInfo.java \
349350
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/ErrorLib.java \
350351
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/LoadResult.java \
351352
./app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/UserDataFormat.java \
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package jp.osscons.opensourcecobol.libcobj.user_util.indexed_file;
2+
3+
class CobolFileKeyInfo {
4+
public int offset;
5+
public int size;
6+
public boolean duplicate;
7+
8+
public CobolFileKeyInfo(int offset, int size, boolean duplicate) {
9+
this.offset = offset;
10+
this.size = size;
11+
this.duplicate = duplicate;
12+
}
13+
}

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/user_util/indexed_file/IndexedFileUtilMain.java

Lines changed: 325 additions & 69 deletions
Large diffs are not rendered by default.

tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ command_line_options_DEPENDENCIES = \
198198

199199
cobj_idx_DEPENDENCIES = \
200200
cobj-idx.at \
201+
cobj-idx.src/create.at \
201202
cobj-idx.src/info.at \
202203
cobj-idx.src/load.at \
203204
cobj-idx.src/unload.at \

tests/Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ command_line_options_DEPENDENCIES = \
737737

738738
cobj_idx_DEPENDENCIES = \
739739
cobj-idx.at \
740+
cobj-idx.src/create.at \
740741
cobj-idx.src/info.at \
741742
cobj-idx.src/load.at \
742743
cobj-idx.src/unload.at \

tests/cobj-idx.at

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
AT_INIT([cobj-idx])
22

3+
m4_include([create.at])
34
m4_include([info.at])
45
m4_include([load.at])
56
m4_include([unload.at])

tests/cobj-idx.src/create.at

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
AT_SETUP([create: basic])
2+
3+
# Normal case: Test for basic INDEXED file creation
4+
AT_CHECK([${COBJ_IDX} create test.idx --size=30 --key=1,5])
5+
6+
# Verify that the created file exists
7+
AT_CHECK([test -f test.idx])
8+
9+
# Check information of the created INDEXED file using info command
10+
AT_CHECK([${COBJ_IDX} info test.idx], [0],
11+
[Size of a record: 30
12+
Number of records: 0
13+
Primary key position: 1-5
14+
])
15+
16+
AT_CLEANUP
17+
18+
AT_SETUP([create: with multiple keys])
19+
20+
# Normal case: Test for INDEXED file creation with multiple keys
21+
AT_CHECK([${COBJ_IDX} create multikey.idx --size=50 --key=1,5:10,3:d20,6], [0], [], [])
22+
23+
# Verify that the created file exists
24+
AT_CHECK([test -f multikey.idx])
25+
26+
# Check information of the created INDEXED file using info command
27+
AT_CHECK([${COBJ_IDX} info multikey.idx], [0],
28+
[Size of a record: 50
29+
Number of records: 0
30+
Primary key position: 1-5
31+
Alternate key position (No duplicate): 10-12
32+
Alternate key position (Duplicates): 20-25
33+
])
34+
35+
AT_CLEANUP
36+
37+
AT_SETUP([create: overwrite existing file])
38+
39+
# Create an existing file first
40+
AT_CHECK([${COBJ_IDX} create overwrite.idx --size=10 --key=1,3])
41+
42+
# Try to overwrite without --new option
43+
AT_CHECK([${COBJ_IDX} create overwrite.idx --size=20 --key=1,5], [1], [],
44+
[error: overwrite.idx already exists. Use -n or --new option to overwrite it.
45+
])
46+
47+
# Overwrite with --new option
48+
AT_CHECK([${COBJ_IDX} create overwrite.idx --size=20 --key=1,5 --new])
49+
50+
# Confirm that the file was overwritten
51+
AT_CHECK([${COBJ_IDX} info overwrite.idx], [0],
52+
[Size of a record: 20
53+
Number of records: 0
54+
Primary key position: 1-5
55+
])
56+
57+
AT_CLEANUP
58+
59+
AT_SETUP([create: error no indexed file])
60+
61+
# Error case: No INDEXED file name is specified
62+
AT_CHECK([${COBJ_IDX} create], [1], [],
63+
[error: no indexed file is specified.
64+
])
65+
66+
AT_CHECK([${COBJ_IDX} create --key=1,3], [1], [],
67+
[error: no indexed file is specified.
68+
])
69+
70+
AT_CHECK([${COBJ_IDX} create --key=1,3:5,2], [1], [],
71+
[error: no indexed file is specified.
72+
])
73+
74+
75+
AT_CLEANUP
76+
77+
AT_SETUP([create: error no record size])
78+
79+
# Error case: No record size is specified
80+
AT_CHECK([${COBJ_IDX} create nosize.idx], [1], [],
81+
[error: no record size is specified.
82+
])
83+
84+
AT_CLEANUP
85+
86+
AT_SETUP([create: error invalid record size])
87+
88+
# Error case: Invalid record size (string)
89+
AT_CHECK([${COBJ_IDX} create invalid.idx --size=abc --key=1,5], [1], [],
90+
[error: invalid record size: abc
91+
])
92+
93+
# Error case: Invalid record size (negative number)
94+
AT_CHECK([${COBJ_IDX} create invalid.idx --size=-10 --key=1,5], [1], [],
95+
[error: invalid record size: -10
96+
])
97+
98+
# Error case: Invalid record size (zero)
99+
AT_CHECK([${COBJ_IDX} create invalid.idx --size=0 --key=1,5], [1], [],
100+
[error: invalid record size: 0
101+
])
102+
103+
AT_CLEANUP
104+
105+
AT_SETUP([create: error no key])
106+
107+
# Error case: No key information is specified
108+
AT_CHECK([${COBJ_IDX} create nokey.idx --size=30], [1], [],
109+
[error: no key information is specified.
110+
])
111+
112+
AT_CLEANUP
113+
114+
AT_SETUP([create: error primary key is duplicate])
115+
116+
# Error case: Primary key is specified as a duplicate key
117+
AT_CHECK([${COBJ_IDX} create dupkey.idx --size=30 --key=d1,5], [1], [],
118+
[error: the first key (primary key) must not be a duplicate key.
119+
])
120+
121+
AT_CLEANUP
122+
123+
AT_SETUP([create: error invalid key format])
124+
125+
# Error case: Invalid key information format
126+
AT_CHECK([${COBJ_IDX} create badkey.idx --size=30 --key=1], [1], [],
127+
[error: invalid key information: 1
128+
])
129+
130+
AT_CHECK([${COBJ_IDX} create badkey.idx --size=30 --key=a,5], [1], [],
131+
[error: invalid key information: a,5
132+
])
133+
134+
AT_CLEANUP
135+
136+
AT_SETUP([create: error key out of range])
137+
138+
# Error case: Key range exceeds record size
139+
AT_CHECK([${COBJ_IDX} create outrange.idx --size=10 --key=5,10], [1], [],
140+
[error: invalid key information: offset=5, size=10, record size=10
141+
])
142+
143+
# Error case: Offset is less than or equal to 0
144+
AT_CHECK([${COBJ_IDX} create badoffset.idx --size=30 --key=0,5], [1], [],
145+
[error: key offsets must be greater than 0: 0,5
146+
])
147+
148+
# Error case: Size is less than or equal to 0
149+
AT_CHECK([${COBJ_IDX} create badsize.idx --size=30 --key=1,0], [1], [],
150+
[error: key sizes must be greater than 0: 1,0
151+
])
152+
153+
AT_CHECK([${COBJ_IDX} create test.idx --size=100 --key=0,2:15,4:d20,5], [1], [],
154+
[error: key offsets must be greater than 0: 0,2:15,4:d20,5
155+
])
156+
157+
AT_CHECK([${COBJ_IDX} create test.idx --size=100 --key=10,2:0,4:d20,5], [1], [],
158+
[error: key offsets must be greater than 0: 10,2:0,4:d20,5
159+
])
160+
161+
AT_CHECK([${COBJ_IDX} create test.idx --size=100 --key=10,2:15,4:d0,5], [1], [],
162+
[error: key offsets must be greater than 0: 10,2:15,4:d0,5
163+
])
164+
165+
AT_CHECK([${COBJ_IDX} create test.idx --size=100 --key=10,0:15,4:d20,5], [1], [],
166+
[error: key sizes must be greater than 0: 10,0:15,4:d20,5
167+
])
168+
169+
AT_CHECK([${COBJ_IDX} create test.idx --size=100 --key=10,2:15,0:d20,5], [1], [],
170+
[error: key sizes must be greater than 0: 10,2:15,0:d20,5
171+
])
172+
173+
AT_CHECK([${COBJ_IDX} create test.idx --size=100 --key=10,2:15,4:d20,0], [1], [],
174+
[error: key sizes must be greater than 0: 10,2:15,4:d20,0
175+
])
176+
177+
AT_CLEANUP
178+
179+
AT_SETUP([create: error overlapping keys])
180+
181+
# Error case: Overlapping key ranges
182+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:3,5 2> /dev/null], [1])
183+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:5,1 2> /dev/null], [1])
184+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:5,2 2> /dev/null], [1])
185+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:5,1 2> /dev/null], [1])
186+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:5,2 2> /dev/null], [1])
187+
188+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:1,6 2> /dev/null], [1])
189+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:2,5 2> /dev/null], [1])
190+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:1,5 2> /dev/null], [1])
191+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:2,4 2> /dev/null], [1])
192+
193+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=3,5:1,5 2> /dev/null], [1])
194+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,1:1,5 2> /dev/null], [1])
195+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,2:1,5 2> /dev/null], [1])
196+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,1:1,5 2> /dev/null], [1])
197+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,2:1,5 2> /dev/null], [1])
198+
199+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,6:2,4 2> /dev/null], [1])
200+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,5:2,4 2> /dev/null], [1])
201+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:2,4 2> /dev/null], [1])
202+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:2,4 2> /dev/null], [1])
203+
204+
###############
205+
206+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:d3,5 2> /dev/null], [1])
207+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:d5,1 2> /dev/null], [1])
208+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:d5,2 2> /dev/null], [1])
209+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:d5,1 2> /dev/null], [1])
210+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:d5,2 2> /dev/null], [1])
211+
212+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:d1,6 2> /dev/null], [1])
213+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:d2,5 2> /dev/null], [1])
214+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:d1,5 2> /dev/null], [1])
215+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:d2,4 2> /dev/null], [1])
216+
217+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=3,5:d1,5 2> /dev/null], [1])
218+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,1:d1,5 2> /dev/null], [1])
219+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,2:d1,5 2> /dev/null], [1])
220+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,1:d1,5 2> /dev/null], [1])
221+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=5,2:d1,5 2> /dev/null], [1])
222+
223+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,6:d2,4 2> /dev/null], [1])
224+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,5:d2,4 2> /dev/null], [1])
225+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=1,5:d2,4 2> /dev/null], [1])
226+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=2,4:d2,4 2> /dev/null], [1])
227+
228+
###############
229+
230+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:1,5:d3,5 2> /dev/null], [1])
231+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:1,5:d5,1 2> /dev/null], [1])
232+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:1,5:d5,2 2> /dev/null], [1])
233+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:1,5:d5,1 2> /dev/null], [1])
234+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:1,5:d5,2 2> /dev/null], [1])
235+
236+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:2,4:d1,6 2> /dev/null], [1])
237+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:2,4:d2,5 2> /dev/null], [1])
238+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:2,4:d1,5 2> /dev/null], [1])
239+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:2,4:d2,4 2> /dev/null], [1])
240+
241+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:3,5:d1,5 2> /dev/null], [1])
242+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:5,1:d1,5 2> /dev/null], [1])
243+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:5,2:d1,5 2> /dev/null], [1])
244+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:5,1:d1,5 2> /dev/null], [1])
245+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:5,2:d1,5 2> /dev/null], [1])
246+
247+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:1,6:d2,4 2> /dev/null], [1])
248+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:2,5:d2,4 2> /dev/null], [1])
249+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:1,5:d2,4 2> /dev/null], [1])
250+
AT_CHECK([${COBJ_IDX} create overlap.idx --size=30 --key=15,5:2,4:d2,4 2> /dev/null], [1])
251+
252+
AT_CLEANUP
253+
254+
AT_SETUP([create: load and unload after creation])
255+
256+
# Normal case: Create an INDEXED file
257+
AT_CHECK([${COBJ_IDX} create loadtest.idx --size=30 --key=1,5])
258+
259+
# Prepare test data for loading
260+
AT_DATA([load_data.txt],
261+
[abcde12345test data record 1**
262+
fghij67890test data record 2**
263+
])
264+
265+
# Load data into the created file (LINE_SEQUENTIAL format)
266+
AT_CHECK([${COBJ_IDX} load loadtest.idx load_data.txt --format=txt])
267+
268+
# Unload and verify the data
269+
AT_CHECK([${COBJ_IDX} unload loadtest.idx --format=txt], [0],
270+
[abcde12345test data record 1**
271+
fghij67890test data record 2**
272+
])
273+
274+
# Test loading and unloading with binary format
275+
AT_DATA([binary_data.bin],
276+
[key0112345binary format 1key0267890binary format 2
277+
])
278+
279+
# Create a new file
280+
AT_CHECK([${COBJ_IDX} create bintest.idx --size=25 --key=1,5])
281+
282+
# Load binary data
283+
AT_CHECK([${COBJ_IDX} load bintest.idx binary_data.bin])
284+
285+
# Unload and verify the data
286+
AT_CHECK([${COBJ_IDX} unload bintest.idx], [0],
287+
[key0112345binary format 1key0267890binary format 2
288+
])
289+
290+
# Test with a file having multiple keys
291+
AT_CHECK([${COBJ_IDX} create multiloadtest.idx --size=30 --key=1,5:10,5:d20,5])
292+
293+
AT_DATA([multikey_data.txt],
294+
[abcde12345hello67890key3@@@@@@
295+
fghij54321world12345key4@@@@@@
296+
klmno12345test 67890key5@@@@@@
297+
])
298+
299+
AT_CHECK([${COBJ_IDX} load multiloadtest.idx multikey_data.txt --format=txt])
300+
301+
# Unload and verify the data
302+
AT_CHECK([${COBJ_IDX} unload multiloadtest.idx --format=txt], [0],
303+
[abcde12345hello67890key3@@@@@@
304+
fghij54321world12345key4@@@@@@
305+
klmno12345test 67890key5@@@@@@
306+
])
307+
308+
AT_CLEANUP

0 commit comments

Comments
 (0)