ソースを参照

Bugfixes & enhancement in CTurmit init

Allowing copy
Yann Weber 5年前
コミット
c55918fd09
6個のファイルの変更84行の追加3行の削除
  1. 2
    1
      gte/world.py
  2. 18
    0
      libs/cturmit.c
  3. 2
    0
      libs/cturmit.h
  4. 36
    2
      libs/turmit.c
  5. 2
    0
      libs/turmit.h
  6. 24
    0
      tests/test_cturmit.py

+ 2
- 1
gte/world.py ファイルの表示

@@ -26,7 +26,8 @@ def eval_prog(args):
26 26
     w = World(args.world_height, args.world_width, gray=True)
27 27
     logger.debug('Running P%d run#%d %s' % (progid, trynum, prog))
28 28
     w.raz()
29
-    turmits = [LivingTurmit(world=w, prog=prog, diag=not args.no_diagonals)
29
+    base = LivingTurmit(world=w, prog=prog, diag=not args.no_diagonals)
30
+    turmits = [ LivingTurmit(world=w, turmit=base)
30 31
                for _ in range(args.turmit_count)]
31 32
 
32 33
     start = time.time()

+ 18
- 0
libs/cturmit.c ファイルの表示

@@ -26,10 +26,17 @@ CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds)
26 26
 	ssize_t arg_stk_sz, arg_int_max;
27 27
 	char *arg_expr;
28 28
 	int i;
29
+	CTurmit *turmit;
29 30
 	PyObject *stack_size, *int_max, *expr, *keys;
30 31
 	PyObject *errtype, *errvalue, *errbck;
31 32
 	
32 33
 	PyErr_Fetch(&errtype, &errvalue, &errbck);
34
+	turmit  = (CTurmit*)PyMapping_GetItemString(kwds, "turmit");
35
+	if(turmit)
36
+	{
37
+		return _CTurmit_copy_init(self, turmit);
38
+	}
39
+	PyErr_Restore(errtype, errvalue, errbck);
33 40
 
34 41
 	stack_size = int_max = expr = NULL;
35 42
 	self->turmit = NULL;
@@ -183,6 +190,17 @@ CTurmit_dealloc(CTurmit *self)
183 190
 	Py_TYPE(self)->tp_free((PyObject*)self);
184 191
 }
185 192
 
193
+static int _CTurmit_copy_init(CTurmit *self, CTurmit *t)
194
+{
195
+	self->turmit = turmit_copy(t->turmit);
196
+	if(!self->turmit)
197
+	{
198
+		//set error
199
+		return -1;
200
+	}
201
+	return 0;
202
+}
203
+
186 204
 PyObject* CTurmit_call(PyObject *func, PyObject *args, PyObject *kwds)
187 205
 {
188 206
 	turmit_int res;

+ 2
- 0
libs/cturmit.h ファイルの表示

@@ -103,6 +103,8 @@ static struct PyGetSetDef CTurmit_getset[] = {
103 103
 	{NULL}  /* Sentinel */
104 104
 };
105 105
 
106
+static int _CTurmit_copy_init(CTurmit *self, CTurmit *t);
107
+
106 108
 static PyObject *
107 109
 CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
108 110
 

+ 36
- 2
libs/turmit.c ファイルの表示

@@ -172,6 +172,40 @@ turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
172 172
 	return turmit;
173 173
 }
174 174
 
175
+turmit_t *turmit_copy(turmit_t *turmit)
176
+{
177
+	turmit_t *res;
178
+	res = malloc(sizeof(turmit_t));
179
+	if(!res)
180
+	{
181
+		perror("Unable to allocate memory for copy");
182
+		return NULL;
183
+	}
184
+	memcpy(res, turmit, sizeof(turmit_t));
185
+	if(turmit->expr)
186
+	{
187
+		res->expr = strdup(turmit->expr);
188
+	}
189
+	if(turmit->stack)
190
+	{
191
+		res->stack = malloc(sizeof(turmit_int) * res->stack_sz);
192
+		memcpy(res->stack, turmit->stack,
193
+			sizeof(turmit_int) * res->stack_sz);
194
+	}
195
+	if(turmit->op_expr)
196
+	{
197
+		res->op_expr = malloc(sizeof(turmit_op_t) * res->op_expr_sz);
198
+		if(!res->op_expr)
199
+		{
200
+			perror("Unable to allocate memory for expression");
201
+			return res;
202
+		}
203
+		memcpy(res->op_expr, turmit->op_expr,
204
+			sizeof(turmit_op_t) * res->op_expr_sz);
205
+	}
206
+	return res;
207
+}
208
+
175 209
 void turmit_clean(turmit_t *turmit)
176 210
 {
177 211
 	if(turmit->err_str) { free(turmit->err_str); }
@@ -276,7 +310,7 @@ int turmit_compile(turmit_t *turmit)
276 310
 
277 311
 		//Checking for values
278 312
 		//hex
279
-		if(strcasecmp("0x", cur) == 0)
313
+		if(strncasecmp("0x", cur, 2) == 0)
280 314
 		{
281 315
 			cur+= 2;
282 316
 			iret = strtol(cur, &endptr, 16);
@@ -298,7 +332,7 @@ int turmit_compile(turmit_t *turmit)
298 332
 		{
299 333
 			iret = strtol(cur, &endptr, 10);
300 334
 			err = errno;
301
-			if(err)
335
+			if(err || endptr == cur)
302 336
 			{
303 337
 				fprintf(stderr, "Invalid constant %s : %s\n",
304 338
 					cur, strerror(err));

+ 2
- 0
libs/turmit.h ファイルの表示

@@ -130,6 +130,8 @@ turmit_t *turmit_alloc(ssize_t stack_sz, ssize_t int_max, const char *expr,
130 130
 turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
131 131
 	const char *expr, unsigned char flags);
132 132
 
133
+turmit_t *turmit_copy(turmit_t *turmit);
134
+
133 135
 void turmit_clean(turmit_t *turmit);
134 136
 
135 137
 /**@brief Free memory of a turmit

+ 24
- 0
tests/test_cturmit.py ファイルの表示

@@ -29,6 +29,16 @@ class TurmitTestCase(unittest.TestCase):
29 29
         r = t(x=2, y=2, r=255, g=0, b=0)
30 30
         self.assertEqual(r, 3)
31 31
 
32
+    def test_copy(self):
33
+        t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
34
+                   stack_size=42)
35
+        t2 = Turmit(turmit=t)
36
+        r = t2(x=1, y=2, r=255, g=255, b=255)
37
+        self.assertEqual(r, 9)
38
+        r = t2(x=2, y=2, r=255, g=0, b=0)
39
+        self.assertEqual(r, 3)
40
+        
41
+
32 42
     def test_init_badargs(self):
33 43
         ''' Test Turmit __init__ bad arguments '''
34 44
         with self.assertRaises(RuntimeError):
@@ -37,6 +47,20 @@ class TurmitTestCase(unittest.TestCase):
37 47
             t = Turmit(prog='foobar')
38 48
 
39 49
 
50
+class TurmitExprCompileTestCase(unittest.TestCase):
51
+    ''' Tests compilatin capabilities of cturmit '''
52
+
53
+    def test_values(self):
54
+        progs = ('1', '0x1', '9', '0x9', '0xf', '0xF', '0x1FFF', '0xFFF1',
55
+        '12345');
56
+        for prog in progs:
57
+            t = Turmit(prog=prog)
58
+
59
+    def test_var(self):
60
+        progs = ('x', 'y', 'r', 'g', 'b')
61
+        for prog in progs:
62
+            t = Turmit(prog=prog)
63
+
40 64
 
41 65
 class TurmitOperationTestCase(unittest.TestCase):
42 66
     

読み込み中…
キャンセル
保存