t_pfrac pfrac_add(t_pfrac f1, t_pfrac f2){ if(f1.c == 0) return f1; if(f2.c == 0) return f2; t_pfrac f; int nod1 = gcd(f1.b, f1.c); int nod2 = gcd(f2.b, f2.c); int s1, s2; (f1.c < 0)?(s1 = -1):(s1 = 1); (f2.c < 0)?(s2 = -1):(s2 = 1); if (s1 == s2) { f.a = f1.a + f2.a; } else { if (f1.a >= f2.a) f.a = f1.a - f2.a; else { f.a = f2.a - f1.a; s2 = s2 * (-1); //ѕри этом знак мен¤етс¤ на противоположный... } } f1.b = f1.b / nod1; f1.c = f1.c / nod1; f2.b = f2.b / nod2; f2.c = f2.c / nod2; f.c = nok(f1.c, f2.c) * s2; f.b = (f.c / f1.c) * f1.b + (f.c / f2.c) * f2.b; f.a += f.b / abs(f.c); f.b = f.b % abs(f.c); return f; };
int cmp_pfrac(t_pfrac f1, t_pfrac f2){ if(f1.c == 0) return 3; if(f2.c == 0) return 3; int s1, s2; (f1.c < 0)?(s1 = -1):(s1 = 1); (f2.c < 0)?(s2 = -1):(s2 = 1); f1.c = f2.c = nok(f1.c, f2.c); f1.b = f1.b + f1.a * abs(f1.c); f2.b = f2.b + f2.a * abs(f2.c); int f1b = s1 * (int)f1.b; int f2b = s2 * (int)f2.b; if (f1b < f2b) {return -1;} if (f1b == f2b) {return 0;} if (f1b > f2b) {return 1;} }
int main() { FILE *fp; int pos; ConfigFile *ini = new ConfigFile(); POOL_MEM *buf; nok(ini->register_items(test_items, 5), "Check bad sizeof ini_items"); ok(ini->register_items(test_items, sizeof(struct ini_items)), "Check sizeof ini_items"); if ((fp = fopen("test.cfg", "w")) == NULL) { exit (1); } fprintf(fp, "# this is a comment\ndatastore=datastore1\nnewhost=\"host1\"\n"); fflush(fp); nok(ini->parse("test.cfg"), "Test missing member"); ini->clear_items(); fprintf(fp, "int64val=12 # with a comment\n"); fprintf(fp, "int64val=10 # with a comment\n"); fprintf(fp, "int32=100\n"); fprintf(fp, "bool=yes\n"); fprintf(fp, "plugin.test=parameter\n"); fflush(fp); ok(ini->parse("test.cfg"), "Test with all members"); ok(ini->items[0].found, "Test presence of char[]"); ok(!strcmp(ini->items[0].val.nameval, "datastore1"), "Test char[]"); ok(ini->items[1].found, "Test presence of char*"); ok(!strcmp(ini->items[1].val.strval, "host1"), "Test char*"); ok(ini->items[2].found, "Test presence of int"); ok(ini->items[2].val.int64val == 10, "Test int"); ok(ini->items[4].val.boolval == true, "Test bool"); ok(ini->items[6].val.int32val == 100, "Test int 32"); alist *list = ini->items[3].val.alistval; nok(ini->items[3].found, "Test presence of alist"); fprintf(fp, "list=a\nlist=b\nlist=c\n"); fflush(fp); ini->clear_items(); ok(ini->parse("test.cfg"), "Test with all members"); list = ini->items[3].val.alistval; ok(ini->items[3].found, "Test presence of alist"); ok(list != NULL, "Test list member"); ok(list->size() == 3, "Test list size"); ok(!strcmp((char *)list->get(0), "a"), "Testing alist[0]"); ok(!strcmp((char *)list->get(1), "b"), "Testing alist[1]"); ok(!strcmp((char *)list->get(2), "c"), "Testing alist[2]"); system("cp -f test.cfg test3.cfg"); fprintf(fp, "pouet='10, 11, 12'\n"); fprintf(fp, "pint=-100\n"); fprintf(fp, "int64val=-100\n"); /* TODO: fix negative numbers */ fflush(fp); ini->clear_items(); ok(ini->parse("test.cfg"), "Test with errors"); nok(ini->items[5].found, "Test presence of positive int"); fclose(fp); ini->clear_items(); ini->free_items(); /* Test */ if ((fp = fopen("test2.cfg", "w")) == NULL) { exit (1); } fprintf(fp, "# this is a comment\n" "optprompt=\"Datastore Name\"\n" "datastore=@NAME@\n" "optprompt=\"New Hostname to create\"\n" "newhost=@STR@\n" "optprompt=\"Some 64 integer\"\n" "optrequired=yes\n" "int64val=@INT64@\n" "list=@ALIST@\n" "bool=@BOOL@\n" "pint64=@PINT64@\n" "pouet=@STR@\n" "int32=@INT32@\n" "plugin.test=@STR@\n" ); fclose(fp); buf = new POOL_MEM(PM_BSOCK); ok(ini->unserialize("test2.cfg"), "Test dynamic parse"); ok(ini->serialize("test4.cfg"), "Try to dump the item table in a file"); ok(ini->serialize(buf) > 0, "Try to dump the item table in a buffer"); ok(ini->parse("test3.cfg"), "Parse test file with dynamic grammar"); ok((pos = ini->get_item("datastore")) == 0, "Check datastore definition"); ok(ini->items[pos].found, "Test presence of char[]"); ok(!strcmp(ini->items[pos].val.nameval, "datastore1"), "Test char[]"); ok(!strcmp(ini->items[pos].comment, "Datastore Name"), "Check comment"); ok(ini->items[pos].required == false, "Check required"); ok((pos = ini->get_item("newhost")) == 1, "Check newhost definition"); ok(ini->items[pos].found, "Test presence of char*"); ok(!strcmp(ini->items[pos].val.strval, "host1"), "Test char*"); ok(ini->items[pos].required == false, "Check required"); ok((pos = ini->get_item("int64val")) == 2, "Check int64val definition"); ok(ini->items[pos].found, "Test presence of int"); ok(ini->items[pos].val.int64val == 10, "Test int"); ok(ini->items[pos].required == true, "Check required"); ok((pos = ini->get_item("bool")) == 4, "Check bool definition"); ok(ini->items[pos].val.boolval == true, "Test bool"); ok(ini->dump_results(buf), "Test to dump results"); printf("<%s>\n", buf); ini->clear_items(); ini->free_items(); report(); delete buf; exit (0); }