Skip to content

Commit 9619d47

Browse files
committed
Fix Java concoredocker to match Python behavior
1 parent 3975bbd commit 9619d47

3 files changed

Lines changed: 149 additions & 18 deletions

File tree

concoredocker.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class concoredocker {
1616
private static String inpath = "/in";
1717
private static String outpath = "/out";
1818
private static Map<String, Object> params = new HashMap<>();
19+
private static int simtime = 0;
1920
private static int maxtime;
2021

2122
public static void main(String[] args) {
@@ -63,19 +64,19 @@ private static Map<String, Object> parseFile(String filename) throws IOException
6364
private static void defaultMaxTime(int defaultValue) {
6465
try {
6566
String content = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.maxtime")));
66-
// changed assumption from map to list for maxtime, as it usually represents a list of time steps
67-
maxtime = ((List<?>) literalEval(content)).size();
68-
} catch (IOException e) {
67+
maxtime = ((Number) literalEval(content)).intValue();
68+
} catch (Exception e) {
6969
maxtime = defaultValue;
7070
}
7171
}
7272

73-
private static void unchanged() {
73+
private static boolean unchanged() {
7474
if (olds.equals(s)) {
7575
s = "";
76-
} else {
77-
olds = s;
76+
return true;
7877
}
78+
olds = s;
79+
return false;
7980
}
8081

8182
private static Object tryParam(String n, Object i) {
@@ -95,9 +96,13 @@ private static Object read(int port, String name, String initstr) {
9596
retrycount++;
9697
}
9798
s += ins;
98-
Object[] inval = ((List<?>) literalEval(ins)).toArray(); // FIXED: Casted to List, converted to Array
99-
int simtime = Math.max((int) inval[0], 0); // assuming simtime is an integer
100-
return inval[1];
99+
List<?> inval = (List<?>) literalEval(ins);
100+
simtime = Math.max(simtime, ((Number) inval.get(0)).intValue());
101+
Object[] val = new Object[inval.size() - 1];
102+
for (int i = 1; i < inval.size(); i++) {
103+
val[i - 1] = inval.get(i);
104+
}
105+
return val;
101106
} catch (IOException | InterruptedException | ClassCastException e) {
102107
return initstr;
103108
}
@@ -110,20 +115,21 @@ private static void write(int port, String name, Object val, int delta) {
110115
if (val instanceof String) {
111116
Thread.sleep(2 * delay);
112117
} else if (!(val instanceof Object[])) {
113-
System.out.println("mywrite must have list or str");
114-
System.exit(1);
118+
System.out.println("write must have list or str");
119+
return;
115120
}
116121
if (val instanceof Object[]) {
117122
Object[] arrayVal = (Object[]) val;
118123
content.append("[")
119-
.append(maxtime + delta)
124+
.append(simtime + delta)
120125
.append(",")
121126
.append(arrayVal[0]);
122127
for (int i = 1; i < arrayVal.length; i++) {
123128
content.append(",")
124129
.append(arrayVal[i]);
125130
}
126131
content.append("]");
132+
simtime += delta;
127133
} else {
128134
content.append(val);
129135
}
@@ -134,13 +140,14 @@ private static void write(int port, String name, Object val, int delta) {
134140
}
135141

136142
private static Object[] initVal(String simtimeVal) {
137-
int simtime = 0;
138143
Object[] val = new Object[] {};
139144
try {
140-
Object[] arrayVal = ((List<?>) literalEval(simtimeVal)).toArray(); // FIXED: Casted to List, converted to Array
141-
simtime = (int) arrayVal[0]; // assuming simtime is an integer
142-
val = new Object[arrayVal.length - 1];
143-
System.arraycopy(arrayVal, 1, val, 0, val.length);
145+
List<?> inval = (List<?>) literalEval(simtimeVal);
146+
simtime = ((Number) inval.get(0)).intValue();
147+
val = new Object[inval.size() - 1];
148+
for (int i = 1; i < inval.size(); i++) {
149+
val[i - 1] = inval.get(i);
150+
}
144151
} catch (Exception e) {
145152
e.printStackTrace();
146153
}

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pytest>=7.0.0
2-
pytest-cov>=4.0.0
2+
pytest-cov>=4.0.0

tests/test_concoredocker.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import os
2+
import sys
3+
import tempfile
4+
5+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6+
import concoredocker
7+
8+
def test_safe_literal_eval_dict():
9+
f = tempfile.NamedTemporaryFile(mode='w', delete=False)
10+
f.write("{'a': 1, 'b': 2}")
11+
f.close()
12+
result = concoredocker.safe_literal_eval(f.name, {})
13+
os.unlink(f.name)
14+
assert result == {'a': 1, 'b': 2}
15+
16+
def test_safe_literal_eval_list():
17+
f = tempfile.NamedTemporaryFile(mode='w', delete=False)
18+
f.write("[1, 2, 3]")
19+
f.close()
20+
result = concoredocker.safe_literal_eval(f.name, [])
21+
os.unlink(f.name)
22+
assert result == [1, 2, 3]
23+
24+
def test_safe_literal_eval_missing():
25+
result = concoredocker.safe_literal_eval("/nonexistent.txt", {'default': True})
26+
assert result == {'default': True}
27+
28+
def test_safe_literal_eval_bad_syntax():
29+
f = tempfile.NamedTemporaryFile(mode='w', delete=False)
30+
f.write("not valid {{{")
31+
f.close()
32+
result = concoredocker.safe_literal_eval(f.name, "fallback")
33+
os.unlink(f.name)
34+
assert result == "fallback"
35+
36+
def test_initval():
37+
concoredocker.simtime = 0
38+
result = concoredocker.initval("[5.0, 1.0, 2.0]")
39+
assert result == [1.0, 2.0]
40+
assert concoredocker.simtime == 5.0
41+
42+
def test_initval_single():
43+
concoredocker.simtime = 0
44+
result = concoredocker.initval("[10.0, 99]")
45+
assert result == [99]
46+
assert concoredocker.simtime == 10.0
47+
48+
def test_unchanged_true():
49+
concoredocker.s = "abc"
50+
concoredocker.olds = "abc"
51+
assert concoredocker.unchanged() == True
52+
assert concoredocker.s == ''
53+
54+
def test_unchanged_false():
55+
concoredocker.s = "new"
56+
concoredocker.olds = "old"
57+
assert concoredocker.unchanged() == False
58+
assert concoredocker.olds == "new"
59+
60+
def test_write_list():
61+
tmpdir = tempfile.mkdtemp()
62+
outdir = os.path.join(tmpdir, "out1")
63+
os.makedirs(outdir)
64+
old_outpath = concoredocker.outpath
65+
concoredocker.outpath = os.path.join(tmpdir, "out")
66+
concoredocker.simtime = 5.0
67+
68+
concoredocker.write(1, "testfile", [1.0, 2.0], delta=0)
69+
70+
with open(os.path.join(outdir, "testfile")) as f:
71+
content = f.read()
72+
assert content == "[5.0, 1.0, 2.0]"
73+
concoredocker.outpath = old_outpath
74+
75+
def test_write_delta():
76+
tmpdir = tempfile.mkdtemp()
77+
outdir = os.path.join(tmpdir, "out1")
78+
os.makedirs(outdir)
79+
old_outpath = concoredocker.outpath
80+
concoredocker.outpath = os.path.join(tmpdir, "out")
81+
concoredocker.simtime = 10.0
82+
83+
concoredocker.write(1, "testfile", [3.0], delta=2)
84+
85+
with open(os.path.join(outdir, "testfile")) as f:
86+
content = f.read()
87+
assert content == "[12.0, 3.0]"
88+
assert concoredocker.simtime == 12.0
89+
concoredocker.outpath = old_outpath
90+
91+
def test_read_file():
92+
tmpdir = tempfile.mkdtemp()
93+
indir = os.path.join(tmpdir, "in1")
94+
os.makedirs(indir)
95+
old_inpath = concoredocker.inpath
96+
concoredocker.inpath = os.path.join(tmpdir, "in")
97+
concoredocker.delay = 0.001
98+
99+
with open(os.path.join(indir, "data"), 'w') as f:
100+
f.write("[7.0, 100, 200]")
101+
102+
concoredocker.s = ''
103+
concoredocker.simtime = 0
104+
result = concoredocker.read(1, "data", "[0, 0, 0]")
105+
106+
assert result == [100, 200]
107+
assert concoredocker.simtime == 7.0
108+
concoredocker.inpath = old_inpath
109+
concoredocker.delay = 1
110+
111+
def test_read_missing():
112+
tmpdir = tempfile.mkdtemp()
113+
indir = os.path.join(tmpdir, "in1")
114+
os.makedirs(indir)
115+
old_inpath = concoredocker.inpath
116+
concoredocker.inpath = os.path.join(tmpdir, "in")
117+
concoredocker.delay = 0.001
118+
119+
concoredocker.s = ''
120+
concoredocker.simtime = 0
121+
result = concoredocker.read(1, "nofile", "[0, 5, 5]")
122+
assert result == [5, 5]
123+
concoredocker.inpath = old_inpath
124+
concoredocker.delay = 1

0 commit comments

Comments
 (0)