source: doc/theses/fangren_yu_MMath/test.adb@ 57c7e6c4

Last change on this file since 57c7e6c4 was 68c7062, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

update overload test programs for Ada and swift

  • Property mode set to 100644
File size: 2.9 KB
Line 
1with Ada.Text_IO; use Ada.Text_IO;
2with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
3
4procedure test is
5 Function Random return Integer is begin return 3; end;
6 Function Random return Float is begin return 3.5; end;
7 Function Random return Unbounded_String is begin return To_Unbounded_String( "abc" ); end;
8
9 Procedure Print( V : Integer ) is begin Put_Line( Integer'Image(V) ); end;
10 Procedure Print( V : Float ) is begin Put_Line( Float'Image(V) ); end;
11 Procedure Print( V : Unbounded_String ) is begin Put_Line( Ada.Strings.Unbounded.To_String(V) ); end;
12
13 Function Func( V : Integer ) return Integer is begin return V; end;
14 Function Func( V : Float ) return Float is begin return V; end;
15 Function Func( V : Unbounded_String ) return Unbounded_String is begin return V; end;
16 Function Func( V1 : Integer; V2 : Float ) return Float is begin return Float(V1) + V2; end;
17
18 subtype Int is Integer;
19 J : Int;
20 Function "-"( L, R : Int ) return Int is begin Put_Line( "X" ); return Integer(L) + (-Integer(R)); end; -- prevent infinite recusion
21
22-- duplicate body for "-" declared at line 20
23-- subtype SInt is Integer range -10 .. 10;
24-- Function "-"( L, R : SInt ) return SInt is begin Put_Line( "X" ); return Integer(L) + (-Integer(R)); end; -- prevent infinite recusion
25
26 i : Integer;
27 f : Float;
28 s : Unbounded_String;
29
30 Type Complex is
31 record
32 Re, Im : Float;
33 end record;
34 c : Complex := (Re => 1.0, Im => 1.0);
35 Procedure Grind (X : Complex) is begin Put_Line( "Grind1" ); end;
36 Procedure Grind (X : Unbounded_String) is begin Put_Line( "Grind2" ); end;
37
38 generic
39 type T is private;
40 with function "+"( X, Y: T ) return T;
41 function twice( X : T ) return T;
42
43 function twice( X: T ) return T is
44 begin
45 Put_Line( "XXX" ); return X + X; -- The formal operator "*".
46 end twice;
47
48 function Int_Twice is new Twice( Integer, "+" => "+" );
49 function float_Twice is new Twice( float, "+" => "+" );
50
51-- generic units cannot be overloaded
52-- generic
53-- type T is private;
54-- with function "+"( X, Y: T ) return T;
55-- function twice( X : T; Y : T ) return T;
56
57-- function twice( X: T; Y : T ) return T is
58-- begin
59-- Put_Line( "XXX" ); return X + X; -- The formal operator "*".
60-- end twice;
61begin
62 I := 3;
63 I := 7 - 3;
64 Print( i );
65 F := 3.8;
66 I := 7 - 2;
67 Print( i );
68 J := 7 - 3;
69 Print( j );
70
71 i := Random;
72 Print( i );
73 f := Random;
74 Print( f );
75 s := Random;
76 Print( s );
77
78 Print( Func( V => 7 ) );
79 Print( Func( 7.5 ) );
80 Print( Func( To_Unbounded_String( "abc" ) ) );
81 Print( Func( 3, 3.5 ) );
82-- Print( Func( 3, 3 ) );
83
84 Grind( X => (Re => 1.0, Im => 1.0) );
85 Grind( c );
86 Grind( To_Unbounded_String( "abc" ) );
87
88 i := Int_Twice( 2 );
89 Put_Line( Integer'Image(i) );
90 f := float_Twice( 2.5 );
91 Print( f );
92end test;
93
94-- Local Variables: --
95-- tab-width: 4 --
96-- compile-command: "gnatmake test.adb" --
97-- End: --
Note: See TracBrowser for help on using the repository browser.