CGPathWriterTest.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // CGPathWriterTC.m
  3. //
  4. // Created by Marcus Rohrmoser on 15.10.10.
  5. // Copyright (c) 2010-2015, Marcus Rohrmoser mobile Software
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without modification, are permitted
  9. // provided that the following conditions are met:
  10. //
  11. // 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  12. // and the following disclaimer.
  13. //
  14. // 2. The software must not be used for military or intelligence or related purposes nor
  15. // anything that's in conflict with human rights as declared in http://www.un.org/en/documents/udhr/ .
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  18. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  23. // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  24. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. //
  26. //
  27. #import <XCTest/XCTest.h>
  28. @implementation XCTestCase(Helpers)
  29. -(NSString *)contentOfFile:(NSString *)name ofType:(NSString *)type
  30. {
  31. NSError *err = nil;
  32. Class clz = [self class];
  33. XCTAssertNotNil(NSStringFromClass(clz), @"ouch", nil);
  34. NSBundle *b = [NSBundle bundleForClass:clz];
  35. NSString *res = [NSString stringWithFormat:@"%@.%@", NSStringFromClass(clz), name, nil];
  36. NSString *path = [b pathForResource:res ofType:type];
  37. NSString *clob = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
  38. XCTAssertNil(err, @"Issue reading %@.%@: %@", res, type, err, nil);
  39. XCTAssertNotNil(clob, @"Issue reading %@.%@", path, type, nil);
  40. return clob;
  41. }
  42. @end
  43. @interface CGPathWriterTest : XCTestCase
  44. @end
  45. #import "CGPathWriter.h"
  46. #import "CGPathReader.h"
  47. #import "PathParser.h"
  48. @implementation CGPathWriterTest
  49. -(void)testWrite
  50. {
  51. CGMutablePathRef p = CGPathCreateMutable();
  52. CGPathMoveToPoint(p, NULL, 1, 2);
  53. CGPathAddLineToPoint(p, NULL, 3, 4);
  54. CGPathAddCurveToPoint(p, NULL, 5, 6, 7, 8, 9, 10);
  55. char *buf = CGPathToCString(p, 0, 0);
  56. // file:///Users/Developer.SnowLeopard/Platforms/iPhoneOS.platform/Developer/Documentation/DocSets/com.apple.adc.documentation.AppleiOS4_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/initWithBytesNoCopy:length:encoding:freeWhenDone:
  57. // initWithBytesNoCopy:length:encoding:freeWhenDone:
  58. XCTAssertEqualObjects(@"M1.000000,2.000000L3.000000,4.000000C5.000000,6.000000,7.000000,8.000000,9.000000,10.000000", ([NSString stringWithCString:buf encoding:NSASCIIStringEncoding]), @"");
  59. free(buf);
  60. CGPathRelease(p);
  61. }
  62. -(void)testReadWrite
  63. {
  64. NSString *ps = @"M1.000000,2.000000L3.000000,4.000000C5.000000,6.000000,7.000000,8.000000,9.000000,10.000000";
  65. PathParser *pp = [[PathParser alloc] init];
  66. NSError *err = nil;
  67. CGPathRef p = [pp newCGPathWithNSString:ps trafo:NULL error:&err];
  68. XCTAssertNil(err, @"");
  69. char *buf = CGPathToCString(p, 0, 0);
  70. // file:///Users/Developer.SnowLeopard/Platforms/iPhoneOS.platform/Developer/Documentation/DocSets/com.apple.adc.documentation.AppleiOS4_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/initWithBytesNoCopy:length:encoding:freeWhenDone:
  71. // initWithBytesNoCopy:length:encoding:freeWhenDone:
  72. XCTAssertEqualObjects(ps, ([NSString stringWithCString:buf encoding:NSASCIIStringEncoding]), @"");
  73. free(buf);
  74. CGPathRelease(p);
  75. }
  76. /** http://www.w3.org/TR/SVG11/paths.html#PathDataCubicBezierCommands
  77. */
  78. -(void)testPathDataCubicBezierCommands
  79. {
  80. NSString *d = @"M100,200 C100,100 250,100 250,200\nS400,300 400,200";
  81. NSError *err = nil;
  82. CGPathRef p = CGPathCreateFromSVGPath(d, NULL, &err);
  83. XCTAssertNil(err, @"");
  84. CGPathRelease(p);
  85. }
  86. @end