Browse Source

Bugfixes & enhancement in CTurmit init

Allowing copy
Yann Weber 6 years ago
parent
commit
c55918fd09
6 changed files with 84 additions and 3 deletions
  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 View File

26
     w = World(args.world_height, args.world_width, gray=True)
26
     w = World(args.world_height, args.world_width, gray=True)
27
     logger.debug('Running P%d run#%d %s' % (progid, trynum, prog))
27
     logger.debug('Running P%d run#%d %s' % (progid, trynum, prog))
28
     w.raz()
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
                for _ in range(args.turmit_count)]
31
                for _ in range(args.turmit_count)]
31
 
32
 
32
     start = time.time()
33
     start = time.time()

+ 18
- 0
libs/cturmit.c View File

26
 	ssize_t arg_stk_sz, arg_int_max;
26
 	ssize_t arg_stk_sz, arg_int_max;
27
 	char *arg_expr;
27
 	char *arg_expr;
28
 	int i;
28
 	int i;
29
+	CTurmit *turmit;
29
 	PyObject *stack_size, *int_max, *expr, *keys;
30
 	PyObject *stack_size, *int_max, *expr, *keys;
30
 	PyObject *errtype, *errvalue, *errbck;
31
 	PyObject *errtype, *errvalue, *errbck;
31
 	
32
 	
32
 	PyErr_Fetch(&errtype, &errvalue, &errbck);
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
 	stack_size = int_max = expr = NULL;
41
 	stack_size = int_max = expr = NULL;
35
 	self->turmit = NULL;
42
 	self->turmit = NULL;
183
 	Py_TYPE(self)->tp_free((PyObject*)self);
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
 PyObject* CTurmit_call(PyObject *func, PyObject *args, PyObject *kwds)
204
 PyObject* CTurmit_call(PyObject *func, PyObject *args, PyObject *kwds)
187
 {
205
 {
188
 	turmit_int res;
206
 	turmit_int res;

+ 2
- 0
libs/cturmit.h View File

103
 	{NULL}  /* Sentinel */
103
 	{NULL}  /* Sentinel */
104
 };
104
 };
105
 
105
 
106
+static int _CTurmit_copy_init(CTurmit *self, CTurmit *t);
107
+
106
 static PyObject *
108
 static PyObject *
107
 CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
109
 CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
108
 
110
 

+ 36
- 2
libs/turmit.c View File

172
 	return turmit;
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
 void turmit_clean(turmit_t *turmit)
209
 void turmit_clean(turmit_t *turmit)
176
 {
210
 {
177
 	if(turmit->err_str) { free(turmit->err_str); }
211
 	if(turmit->err_str) { free(turmit->err_str); }
276
 
310
 
277
 		//Checking for values
311
 		//Checking for values
278
 		//hex
312
 		//hex
279
-		if(strcasecmp("0x", cur) == 0)
313
+		if(strncasecmp("0x", cur, 2) == 0)
280
 		{
314
 		{
281
 			cur+= 2;
315
 			cur+= 2;
282
 			iret = strtol(cur, &endptr, 16);
316
 			iret = strtol(cur, &endptr, 16);
298
 		{
332
 		{
299
 			iret = strtol(cur, &endptr, 10);
333
 			iret = strtol(cur, &endptr, 10);
300
 			err = errno;
334
 			err = errno;
301
-			if(err)
335
+			if(err || endptr == cur)
302
 			{
336
 			{
303
 				fprintf(stderr, "Invalid constant %s : %s\n",
337
 				fprintf(stderr, "Invalid constant %s : %s\n",
304
 					cur, strerror(err));
338
 					cur, strerror(err));

+ 2
- 0
libs/turmit.h View File

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

+ 24
- 0
tests/test_cturmit.py View File

29
         r = t(x=2, y=2, r=255, g=0, b=0)
29
         r = t(x=2, y=2, r=255, g=0, b=0)
30
         self.assertEqual(r, 3)
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
     def test_init_badargs(self):
42
     def test_init_badargs(self):
33
         ''' Test Turmit __init__ bad arguments '''
43
         ''' Test Turmit __init__ bad arguments '''
34
         with self.assertRaises(RuntimeError):
44
         with self.assertRaises(RuntimeError):
37
             t = Turmit(prog='foobar')
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
 class TurmitOperationTestCase(unittest.TestCase):
65
 class TurmitOperationTestCase(unittest.TestCase):
42
     
66
     

Loading…
Cancel
Save