DDAtomicCounterTests.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. @import XCTest;
  16. #import <CocoaLumberjack/DDDispatchQueueLogFormatter.h>
  17. @interface DDAtomicCounterTests : XCTestCase
  18. @end
  19. @implementation DDAtomicCounterTests
  20. - (void)testSimpleAtomicCounter {
  21. #pragma clang diagnostic push
  22. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  23. __auto_type atomicCounter = [[DDAtomicCounter alloc] initWithDefaultValue:0];
  24. #pragma clang diagnostic pop
  25. XCTAssertEqual([atomicCounter value], 0);
  26. XCTAssertEqual([atomicCounter increment], 1);
  27. XCTAssertEqual([atomicCounter value], 1);
  28. XCTAssertEqual([atomicCounter decrement], 0);
  29. XCTAssertEqual([atomicCounter value], 0);
  30. }
  31. - (void)testMultithreadAtomicCounter {
  32. #pragma clang diagnostic push
  33. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  34. __auto_type atomicCounter = [[DDAtomicCounter alloc] initWithDefaultValue:0];
  35. #pragma clang diagnostic pop
  36. __auto_type expectation = [self expectationWithDescription:@"Multithread atomic counter"];
  37. __auto_type globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  38. static NSInteger numberOfThreads = 5;
  39. expectation.expectedFulfillmentCount = numberOfThreads;
  40. for (NSInteger i = 0; i < numberOfThreads; i++) {
  41. dispatch_async(globalQueue, ^{
  42. [atomicCounter increment];
  43. XCTAssertGreaterThanOrEqual([atomicCounter value], 1);
  44. [expectation fulfill];
  45. });
  46. }
  47. [self waitForExpectationsWithTimeout:5.0 handler:^(NSError * _Nullable error) {
  48. XCTAssertNil(error);
  49. XCTAssertEqual([atomicCounter value], numberOfThreads);
  50. }];
  51. }
  52. - (void)testMultithreadAtomicCounterWithIncrementAndDecrement {
  53. #pragma clang diagnostic push
  54. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  55. __auto_type atomicCounter = [[DDAtomicCounter alloc] initWithDefaultValue:0];
  56. #pragma clang diagnostic pop
  57. __auto_type expectation = [self expectationWithDescription:@"Multithread atomic counter inc and dec"];
  58. __auto_type globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  59. static NSInteger numberOfThreads = 5;
  60. expectation.expectedFulfillmentCount = numberOfThreads * 2;
  61. for (NSInteger i = 0; i < numberOfThreads; i++) {
  62. dispatch_async(globalQueue, ^{
  63. [atomicCounter increment];
  64. [expectation fulfill];
  65. });
  66. dispatch_async(globalQueue, ^{
  67. [atomicCounter decrement];
  68. [expectation fulfill];
  69. });
  70. }
  71. [self waitForExpectationsWithTimeout:5.0 handler:^(NSError * _Nullable error) {
  72. XCTAssertNil(error);
  73. XCTAssertEqual([atomicCounter value], 0);
  74. }];
  75. }
  76. @end