DEFINEFN bool PsycoSequence_SetItem(PsycoObject* po, vinfo_t* o, vinfo_t* i, vinfo_t* value) { PySequenceMethods *m; PyTypeObject* tp = Psyco_NeedType(po, o); if (tp == NULL) return false; m = tp->tp_as_sequence; if (m && m->sq_ass_item) { bool result; char* vargs; vinfo_t* release_me = NULL; if (m->sq_length) { condition_code_t cc = integer_cmp_i(po, i, 0, Py_LT); if (cc == CC_ERROR) return false; if (runtime_condition_f(po, cc)) { vinfo_t* l = Psyco_META1(po, m->sq_length, CfReturnNormal|CfPyErrIfNeg, "v", o); if (l == NULL) return false; release_me = i = integer_add(po, i, l, false); vinfo_decref(l, po); if (i == NULL) return false; } else assert_nonneg(i); } vargs = (value!=NULL) ? "vvv" : "vvl"; result = Psyco_META3(po, m->sq_ass_item, CfNoReturnValue|CfPyErrIfNonNull, vargs, o, i, value) != NULL; vinfo_xdecref(release_me, po); return result; } type_error(po, (value!=NULL) ? "object doesn't support item assignment" : "object doesn't support item deletion"); return false; }
DEFINEFN vinfo_t* PsycoSequence_GetItem(PsycoObject* po, vinfo_t* o, vinfo_t* i) { PySequenceMethods *m; PyTypeObject* tp = Psyco_NeedType(po, o); if (tp == NULL) return NULL; m = tp->tp_as_sequence; if (m && m->sq_item) { vinfo_t* result; vinfo_t* release_me = NULL; if (m->sq_length) { condition_code_t cc = integer_cmp_i(po, i, 0, Py_LT); if (cc == CC_ERROR) return NULL; if (runtime_condition_f(po, cc)) { vinfo_t* l = Psyco_META1(po, m->sq_length, CfReturnNormal|CfPyErrIfNeg, "v", o); if (l == NULL) return NULL; release_me = i = integer_add(po, i, l, false); vinfo_decref(l, po); if (i == NULL) return NULL; } else assert_nonneg(i); } result = Psyco_META2(po, m->sq_item, CfReturnRef|CfPyErrIfNull, "vv", o, i); vinfo_xdecref(release_me, po); return result; } return type_error(po, "unindexable object"); }
DEFINEFN vinfo_t* PsycoSequence_GetSlice(PsycoObject* po, vinfo_t* o, vinfo_t* i1, vinfo_t* i2) { PySequenceMethods *m; PyTypeObject* tp = Psyco_NeedType(po, o); if (tp == NULL) return false; m = tp->tp_as_sequence; if (m && m->sq_slice) { vinfo_t* result = NULL; vinfo_t* l = NULL; /* length, if already computed */ vinfo_t* release_me1 = NULL; vinfo_t* release_me2 = NULL; if (m->sq_length) { condition_code_t cc; cc = integer_cmp_i(po, i1, 0, Py_LT); if (cc == CC_ERROR) goto fail; if (runtime_condition_f(po, cc)) { /* i1 < 0 */ l = Psyco_META1(po, m->sq_length, CfReturnNormal|CfPyErrIfNeg, "v", o); if (l == NULL) goto fail; release_me1 = i1 = integer_add(po, i1, l, false); if (i1 == NULL) goto fail; } else assert_nonneg(i1); cc = integer_cmp_i(po, i2, 0, Py_LT); if (cc == CC_ERROR) goto fail; if (runtime_condition_f(po, cc)) { /* i2 < 0 */ if (l == NULL) { l = Psyco_META1(po, m->sq_length, CfReturnNormal|CfPyErrIfNeg, "v", o); if (l == NULL) goto fail; } release_me2 = i2 = integer_add(po, i2, l, false); if (i2 == NULL) goto fail; } else assert_nonneg(i2); } result = Psyco_META3(po, m->sq_slice, CfReturnRef|CfPyErrIfNull, "vvv", o, i1, i2); fail: vinfo_xdecref(release_me2, po); vinfo_xdecref(release_me1, po); vinfo_xdecref(l, po); return result; } else { /* XXX call mp_subscript with sliceobj_from_intint */ /* fallback */ return psyco_generic_call(po, PySequence_GetSlice, CfReturnRef|CfPyErrIfNull, "vvv", o, i1, i2); } }
bool test_integer() { char *endptr, buf[64]; struct integer_t *val, *tmp; unsigned int rem; printf("testing integer... "); val = integer_zero(); integer_delete(val); val = integer_zero(); integer_add_uint(&val, 58); if((val->len != 1) || (val->arr[0] != 58)) return printf("failed\n"), false; integer_add_uint(&val, UINT_MAX); if((val->len != 2) || (val->arr[0] != 57) || (val->arr[1] != 1)) return printf("failed\n"), false; integer_delete(val); val = integer_new(14); integer_mul_ushort(&val, 11); if((val->len != 1) || (val->arr[0] != 154)) return printf("failed\n"), false; integer_mul_ushort(&val, 9261); if((val->len != 1) || (val->arr[0] != 1426194)) return printf("failed\n"), false; integer_delete(val); val = integer_new(4194314); rem = integer_div_ushort(&val, 20460); if((rem != 14) || (val->len != 1) || (val->arr[0] != 205)) return printf("failed\n"), false; rem = integer_div_ushort(&val, 3); if((rem != 1) || (val->len != 1) || (val->arr[0] != 68)) return printf("failed\n"), false; integer_delete(val); val = integer_parse("123", &endptr); if((val->len != 1) || val->neg || (val->arr[0] != 123)) return printf("failed\n"), false; integer_delete(val); val = integer_parse("-0x123", &endptr); if((val->len != 1) || !val->neg || (val->arr[0] != 0x123)) return printf("failed\n"), false; integer_delete(val); val = integer_new(78910); str_printf(buf, "%C", integer_chunk(val)); integer_delete(val); if(!str_isequal(buf, "78910")) return printf("failed\n"), false; val = integer_parse("4052555153018976267", NULL); tmp = integer_parse("4656612873077392578125", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "4660665428230411554392")) return printf("failed\n"), false; val = integer_parse("13", NULL); tmp = integer_parse("28", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "41")) return printf("failed\n"), false; val = integer_parse("28", NULL); tmp = integer_parse("13", NULL); integer_sub(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "15")) return printf("failed\n"), false; val = integer_parse("4656612873077392578125", NULL); tmp = integer_parse("4052555153018976267", NULL); integer_sub(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "4652560317924373601858")) return printf("failed\n"), false; val = integer_parse("483306625", NULL); tmp = integer_parse("-3916345896", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "-3433039271")) return printf("failed\n"), false; val = integer_parse("483306625", NULL); tmp = integer_parse("-13582478396", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "-13099171771")) return printf("failed\n"), false; val = integer_new(78910); if(!integer_uint32chk(val)) return printf("failed\n"), false; else if(integer_uint16chk(val)) return printf("failed\n"), false; else if(integer_uint8chk(val)) return printf("failed\n"), false; else if(integer_uint32(val) != 78910) return printf("failed\n"), false; else if(integer_uint16(val) != 13374) return printf("failed\n"), false; else if(integer_uint8(val) != 62) return printf("failed\n"), false; integer_delete(val); val = integer_new(-281); if(!integer_uint16chk(val)) return printf("failed\n"), false; else if(!integer_uint8chk(val)) return printf("failed\n"), false; else if(integer_uint32(val) != 4294967015) return printf("failed\n"), false; else if(integer_uint16(val) != 65255) return printf("failed\n"), false; else if(integer_uint8(val) != 231) return printf("failed %u\n", integer_uint8(val)), false; integer_delete(val); printf("okay\n"); return true; }
void operator_integer() { int result; char in[USHRT_MAX]; printf("Entre com o 1º valor: "); scanf("%s", in); printf("\n"); result = atoi(in); int keep_going = 1; while (keep_going) { switch (menu_integer()) { case 1: // Add printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_add(&result, atoi(in)); break; case 2: // Subtract printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_subtract(&result, atoi(in)); break; case 3: // Multiply printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_multiply(&result, atoi(in)); break; case 4: // Power printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_power(&result, atoi(in)); break; case 5: // Divide while (1) { printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); if (atoi(in) == 0) printf("Valor inválido!\n\n"); else break; } integer_divide(&result, atoi(in)); break; case 6: // Module printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_module(&result, atoi(in)); break; default: keep_going = 0; break; } printf("Resultado: %d\n\n", result); } }