source: git/Tst/Short/bug_newstruct2.tst @ e9478b

spielwiese
Last change on this file since e9478b was e9478b, checked in by Hans Schoenemann <hannes@…>, 9 years ago
format
  • Property mode set to 100644
File size: 2.1 KB
Line 
1LIB "tst.lib";
2tst_init();
3
4proc divisor_print(divisor D)
5{
6"Divisor = ("+string(D.den)+") - ("+string(D.num)+")";
7}
8proc divisor_status(divisor D)
9{
10  return ("status");
11}
12proc divisor_test(divisor D)
13{
14  return ("test");
15}
16proc divisor_gcd(divisor D, divisor D2)
17{
18  return ("gcd");
19}
20proc divisor_extgcd(divisor D, divisor D2)
21{
22  return ("extgcd");
23}
24proc divisor_diff(divisor D, divisor D2)
25{
26  return ("diff");
27}
28
29newstruct("divisor","ideal den,ideal num");
30newstruct("formaldivisor","list summands");
31
32system("install","divisor","status",divisor_status, 4);
33system("install","divisor","test",divisor_test,4);
34system("install","divisor","gcd",divisor_gcd,2);
35system("install","divisor","extgcd",divisor_extgcd,2);
36
37
38system("install","divisor","print",divisor_print,1);
39
40ring r=0,(x,y),dp;
41
42divisor C;
43C.num = ideal(x);
44C.den = ideal(y);
45
46C;
47test(C);
48status(C);
49gcd(C,C);
50extgcd(C,C);
51// -------------------------------------------------------
52newstruct("myunion", "poly p,int i");
53proc to_poly(myunion uni)
54{
55  return (uni.p);
56}
57
58proc to_int(myunion uni)
59{
60  return (uni.i);
61}
62
63ring r1=0,x,dp;
64
65myunion uni;
66uni.p = x+1;
67uni.i = 17;
68
69system("install", "myunion", "poly", to_poly, 1);
70system("install", "myunion", "int", to_int, 1);
71
72poly(uni); // -> x+1
73int(uni);   // -> 17
74
75// ---------------------------------------------------------
76
77newstruct("stringifiable", "int i");
78stringifiable stry;
79stry;
80string(stry);
81
82proc to_str(def arg) { return ("(overloaded)"); }
83system("install", "stringifiable", "string", to_str,1);
84stry;  // print falls back to string
85string(stry);
86
87proc to_str_wrong(def arg) { return (17); }
88system("install", "stringifiable", "string", to_str_wrong,1);
89stry;  // incorrectly converted, fall back to default
90string(stry);
91
92proc printing(def arg) { ("(overloaded)"); }
93system("install", "stringifiable", "print", printing,1);
94
95stry;  // now only print works correctly
96string(stry); // incorrectly converted, fall back to default
97
98// try dumb things:
99newstruct("TBoom",        "TUndefined boom , int something");
100newstruct("TWrong",       "int value, TUndefined undefined");
101
102TWrong f;
103type(f.undefined);
104TBoom g;
105
106tst_status(1);$
Note: See TracBrowser for help on using the repository browser.