literals.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // TEST_CFLAGS -framework Foundation
  2. #import <Foundation/Foundation.h>
  3. #import <Foundation/NSDictionary.h>
  4. #import <objc/runtime.h>
  5. #import <objc/objc-abi.h>
  6. #import <math.h>
  7. #include "test.h"
  8. int main() {
  9. PUSH_POOL {
  10. #if __has_feature(objc_bool) // placeholder until we get a more precise macro.
  11. NSArray *array = @[ @1, @2, @YES, @NO, @"Hello", @"World" ];
  12. testassert([array count] == 6);
  13. NSDictionary *dict = @{ @"Name" : @"John Q. Public", @"Age" : @42 };
  14. testassert([dict count] == 2);
  15. NSDictionary *numbers = @{ @"π" : @M_PI, @"e" : @M_E };
  16. testassert([[numbers objectForKey:@"π"] doubleValue] == M_PI);
  17. testassert([[numbers objectForKey:@"e"] doubleValue] == M_E);
  18. BOOL yesBool = YES;
  19. BOOL noBool = NO;
  20. array = @[
  21. @(true),
  22. @(YES),
  23. [NSNumber numberWithBool:YES],
  24. @YES,
  25. @(yesBool),
  26. @((BOOL)YES),
  27. @(false),
  28. @(NO),
  29. [NSNumber numberWithBool:NO],
  30. @NO,
  31. @(noBool),
  32. @((BOOL)NO),
  33. ];
  34. NSData * jsonData = [NSJSONSerialization dataWithJSONObject:array options:0 error:nil];
  35. NSString * string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  36. #if __cplusplus
  37. testassert([string isEqualToString:@"[true,true,true,true,true,true,false,false,false,false,false,false]"]);
  38. #else
  39. // C99 @(true) and @(false) evaluate to @(1) and @(0).
  40. testassert([string isEqualToString:@"[1,true,true,true,true,true,0,false,false,false,false,false]"]);
  41. #endif
  42. #endif
  43. } POP_POOL;
  44. succeed(__FILE__);
  45. return 0;
  46. }