File: basic.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (153 lines) | stat: -rw-r--r-- 5,090 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSAffineTransform.h>

#include <math.h>
static BOOL eq(double d1, double d2)
{
  if (abs(d1 - d2) < 0.000001)
    return YES;
  return NO;
}

static BOOL
is_equal_struct(NSAffineTransformStruct as, NSAffineTransformStruct bs)
{
  if (eq(as.m11, bs.m11) && eq(as.m12, bs.m12) && eq(as.m21, bs.m21)
    && eq(as.m22, bs.m22) && eq(as.tX, bs.tX) && eq(as.tY, bs.tY))
    return YES;
  return NO;
}

#if 0
static void
print_matrix (const char *str, NSAffineTransformStruct MM)
{
  printf("%s = %f %f %f %f  %f %f\n", str, MM.m11, MM.m12,
  	MM.m21, MM.m22, MM.tX, MM.tY);
}
#endif

int main()
{
  NSAutoreleasePool   	*arp = [NSAutoreleasePool new];
  NSAffineTransform 	*testObj;
  NSAffineTransformStruct flip = {1.0,0.0,0.0,-1.0,0.0,0.0};
  NSMutableArray 	*testObjs = [NSMutableArray array];
  NSAffineTransform *aa, *bb, *cc;
  NSAffineTransformStruct as = {2, 3, 4, 5, 10, 20};
  NSAffineTransformStruct bs = {6, 7, 8, 9, 14, 15};
  NSAffineTransformStruct cs;
  NSAffineTransformStruct answer1 = 
    {36.000000, 41.000000, 64.000000, 73.000000,  234.000000, 265.000000};
  NSAffineTransformStruct answer2 = 
    {40.000000, 53.000000, 52.000000, 69.000000,  98.000000, 137.000000};
  NSAffineTransformStruct answer3 = 
    {6.000000, 9.000000, 8.000000, 10.000000,  10.000000, 20.000000};
  NSAffineTransformStruct answer4 = 
    {6.000000, 9.000000, 8.000000, 10.000000,  194.000000, 268.000000};
  NSAffineTransformStruct answer5 = 
    {2.172574, 3.215242, 3.908954, 4.864383,  10.000000, 20.000000};
  NSAffineTransformStruct answer6 = 
    {2.172574, 3.215242, 3.908954, 4.864383,  90.796249, 126.684265};
  NSAffineTransformStruct answer7 = 
    {1.651156, 2.443584, 1.329044, 1.653890,  90.796249, 126.684265};
  NSPoint	p;
  NSSize	s;

  testObj = AUTORELEASE([NSAffineTransform new]);
  [testObjs addObject:testObj];
  PASS(testObj != nil, "can create a new transfor");
   
  test_NSObject(@"NSAffineTransform", testObjs);
  test_NSCoding(testObjs);
  test_keyed_NSCoding(testObjs);
  test_NSCopying(@"NSAffineTransform", @"NSAffineTransform", testObjs, NO, YES);
  
  testObj = [NSAffineTransform transform];
  PASS(testObj != nil, "can create an autoreleased transform");

  [testObj setTransformStruct: flip];
  p = [testObj transformPoint: NSMakePoint(10,10)];
  PASS(eq(p.x, 10) && eq(p.y, -10), "flip transform inverts point y");

  s = [testObj transformSize: NSMakeSize(10,10)];
  PASS(s.width == 10 && s.height == -10, "flip transform inverts size height");

  p = [testObj transformPoint: p];
  s = [testObj transformSize: s];
  PASS(eq(p.x, 10) && eq(p.y, 10) && s.width == 10 && s.height == 10,
    "flip is reversible");
  
  testObj = [NSAffineTransform transform];
  [testObj translateXBy: 5.0 yBy: 6.0];
  p = [testObj transformPoint: NSMakePoint(10,10)];
  PASS(eq(p.x, 15.0) && eq(p.y, 16.0), "simple translate works");

  [testObj translateXBy: 5.0 yBy: 4.0];
  p = [testObj transformPoint: NSMakePoint(10,10)];
  PASS(eq(p.x, 20.0) && eq(p.y, 20.0), "two simple translates work");
  
  [testObj rotateByDegrees: 90.0];
  p = [testObj transformPoint: NSMakePoint(10,10)];
  PASS(eq(p.x, 0.0) && eq(p.y, 20.0), "translate and rotate works");
  
  testObj = [NSAffineTransform transform];

  [testObj rotateByDegrees: 90.0];
  p = [testObj transformPoint: NSMakePoint(10,10)];
  PASS(eq(p.x, -10.0) && eq(p.y, 10.0), "simple rotate works");
  
  [testObj translateXBy: 5.0 yBy: 6.0];
  p = [testObj transformPoint: NSMakePoint(10,10)];
  PASS(eq(p.x, -16.0) && eq(p.y, 15.0), "rotate and translate works");


  aa = [NSAffineTransform transform];
  bb = [NSAffineTransform transform];
  [aa setTransformStruct: as];
  [bb setTransformStruct: bs];

  /* Append matrix */
  cc = AUTORELEASE([aa copy]);
  [cc appendTransform: bb];
  cs = [cc transformStruct];
  PASS((is_equal_struct(cs, answer1)), "appendTransform:")

  /* Prepend matrix */
  cc = AUTORELEASE([aa copy]);
  [cc prependTransform: bb];
  cs = [cc transformStruct];
  PASS((is_equal_struct(cs, answer2)), "prependTransform:")

  /* scaling */
  cc = AUTORELEASE([aa copy]);
  [cc scaleXBy: 3 yBy: 2];
  cs = [cc transformStruct];
  PASS((is_equal_struct(cs, answer3)), "scaleXBy:yBy:")
  //print_matrix ("Scale X A", cs);
  [cc translateXBy: 12 yBy: 14];
  cs = [cc transformStruct];
  PASS((is_equal_struct(cs, answer4)), "translateXBy:yBy:")
  //print_matrix ("Trans X Scale X A", cs);

  /* rotation */
  cc = AUTORELEASE([aa copy]);
  [cc rotateByDegrees: 2.5];
  cs = [cc transformStruct];
  PASS((is_equal_struct(cs, answer5)), "rotateByDegrees")
  //print_matrix ("Rotate X A", cs);
  [cc translateXBy: 12 yBy: 14];
  cs = [cc transformStruct];
  PASS((is_equal_struct(cs, answer6)), "Translate X Rotate X A")
  //print_matrix ("Trans X Rotate X A", cs);

  /* multiple */
  [cc scaleXBy: .76 yBy: .34];
  cs = [cc transformStruct];
  PASS((is_equal_struct(cs, answer7)), "Scale X Translate X Rotate X A")
  //print_matrix ("Scale X Trans X Rotate X A", cs);

  [arp release]; arp = nil;
  return 0;
}