source: git/Tst/Short/bug_newstruct3.tst @ 0f2fcb4

spielwiese
Last change on this file since 0f2fcb4 was 3877fe, checked in by Alexander Dreyer <alexander.dreyer@…>, 12 years ago
fix: newstruct's string operator may be overloaded
  • 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");
31newstruct("pdivisor","list summands, cone tail");
32
33system("install","divisor","status",divisor_status, 4);
34system("install","divisor","test",divisor_test,4);
35system("install","divisor","gcd",divisor_gcd,2);
36system("install","divisor","extgcd",divisor_extgcd,2);
37
38
39system("install","divisor","print",divisor_print,1);
40
41ring r=0,(x,y),dp;
42
43divisor C;
44C.num = ideal(x);
45C.den = ideal(y);
46
47C;
48test(C);
49status(C);
50gcd(C,C);
51extgcd(C,C);
52// -------------------------------------------------------
53newstruct("myunion", "poly p,int i");
54proc to_poly(myunion uni)           
55{                                   
56  return (uni.p);                   
57}                                   
58
59proc to_int(myunion uni)
60{                       
61  return (uni.i);       
62}                       
63
64ring r=0,x,dp;
65
66myunion uni;
67uni.p = x+1;
68uni.i = 17;
69
70system("install", "myunion", "poly", to_poly, 1);
71system("install", "myunion", "int", to_int, 1); 
72
73poly(uni); // -> x+1
74int(uni);   // -> 17
75
76// ---------------------------------------------------------
77
78newstruct("stringifiable", "int i");
79stringifiable stry;
80stry;
81string(stry);
82
83proc to_str(def arg) { return ("(overloaded)"); }
84system("install", "stringifiable", "string", to_str,1);
85stry;  // print falls back to string
86string(stry);
87
88proc to_str_wrong(def arg) { return (17); }
89system("install", "stringifiable", "string", to_str_wrong,1);
90stry;  // incorrectly converted, fall back to default
91string(stry);
92
93proc printing(def arg) { ("(overloaded)"); }
94system("install", "stringifiable", "print", printing,1);
95
96stry;  // now only print works correctly
97string(stry); // incorrectly converted, fall back to default
98
99tst_status(1);$
Note: See TracBrowser for help on using the repository browser.