Skip to content

Commit

Permalink
Add tests for memoryPhysicalFootprint and `memoryPhysicalFootprintS…
Browse files Browse the repository at this point in the history
…tring`
  • Loading branch information
thecatalinstan committed Jun 18, 2021
1 parent b6b837a commit 2b0bcd4
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ FOUNDATION_EXPORT NSString * const CSSystemInfoHelperIPAddressNone DEPRECATED_AT
/// Gets the physical memory footprint size of the process, as reported by
/// @c task_info
// @see https://www.gnu.org/software/hurd/gnumach-doc/Task-Information.html
@property (nonatomic, readonly) vm_size_t memotyPhysicalFootprint;
@property (nonatomic, readonly) vm_size_t memoryPhysicalFootprint;

/// A human-readable, formatted byte count string. from the value returned by
/// @c -memotyPhysicalFootprint
@property (nonatomic, readonly, copy) NSString *memotyPhysicalFootprintString;
/// @c -memoryPhysicalFootprint
@property (nonatomic, readonly, copy) NSString *memoryPhysicalFootprintString;

/// @name UUID of the current device

Expand Down
12 changes: 6 additions & 6 deletions CSSystemInfoHelper/Sources/CSSystemInfoHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,18 @@ - (NSString *)memoryUsageString {
return [self formatByteCount:self.memoryUsage];
}

- (vm_size_t)memotyPhysicalFootprint {
- (vm_size_t)memoryPhysicalFootprint {
NSError *error;
vm_size_t memotyPhysicalFootprint = 0;
if (![self.systemInfoProvider getPhysFootprint:&memotyPhysicalFootprint error:&error]) {
vm_size_t memoryPhysicalFootprint = 0;
if (![self.systemInfoProvider getPhysFootprint:&memoryPhysicalFootprint error:&error]) {
NSLog(@"Error getting physical memory footprint: %@. %@.", error.localizedDescription, error.localizedFailureReason);
return 0;
}
return memotyPhysicalFootprint;
return memoryPhysicalFootprint;
}

- (NSString *)memotyPhysicalFootprintString {
return [self formatByteCount:self.memotyPhysicalFootprint];
- (NSString *)memoryPhysicalFootprintString {
return [self formatByteCount:self.memoryPhysicalFootprint];
}

#if TARGET_OS_OSX
Expand Down
160 changes: 148 additions & 12 deletions CSSystemInfoHelperTests/CSSystemInfoHelperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ @interface CSSystemInfoHelperTests : XCTestCase

@implementation CSSystemInfoHelperTests

#pragma mark - sharedHelper

- (void)test_sharedHelper_ShouldNotBeNil {
XCTAssertNotNil([CSSystemInfoHelper sharedHelper]);
}
Expand All @@ -27,6 +29,8 @@ - (void)test_sharedHelper_IsSigleton {
XCTAssertEqual(helper1.hash, helper2.hash);
}

#pragma mark - networkInterfaces

- (void)test_networkInterfaces_withFailingSystemInfoProvider_ShouldBeNil {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];
Expand All @@ -42,6 +46,8 @@ - (void)test_networkInterfaces_withSucceedingSystemInfoProvider_ShouldReturnProv
XCTAssertEqual(expectedInterfaces, helper.networkInterfaces);
}

#pragma mark - systemInfo

- (void)test_SystemInfo_DoesNotThow {
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:nil];
XCTAssertNoThrow(helper.systemInfo);
Expand All @@ -67,6 +73,8 @@ - (void)test_SystemInfo_AllKeys_ShouldNotBeEmpty {
XCTAssertGreaterThan(helper.systemInfo[CSSystemInfoKeyMachine].length, 0, @"The key CSSystemInfoKeyMachine is empty");
}

#pragma mark - systemInfoString

- (void)test_SystemInfoString_DoesNotThrow {
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:nil];
XCTAssertNoThrow(helper.systemInfoString);
Expand All @@ -82,6 +90,8 @@ - (void)test_SystemInfoString_ShouldNotBeEmpty {
XCTAssertGreaterThan(helper.systemInfoString.length, 0);
}

#pragma mark - systemVersionString

- (void)test_SystemVersionString_DoesNotThrow {
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:nil];
XCTAssertNoThrow(helper.systemVersionString);
Expand All @@ -97,6 +107,8 @@ - (void)test_SystemVersionString_ShouldNotBeEmpty {
XCTAssertGreaterThan(helper.systemVersionString.length, 0);
}

#pragma mark - memoryUsage

- (void)test_memoryUsage_withFailingSystemInfoProvider_ShouldBeZero {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];
Expand All @@ -120,91 +132,93 @@ - (void)test_memoryUsage_withSucceedingSystemInfoProvider_ShouldReturnProvidedVa
XCTAssertEqual(helper.memoryUsage, residentSize);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProvider_DoesNotThrow {
#pragma mark - memoryUsageString

- (void)test_memoryUsageString_withFailingSystemInfoProvider_DoesNotThrow {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNoThrow(helper.memoryUsageString);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProvider_ShouldNotBeNil {
- (void)test_memoryUsageString_withFailingSystemInfoProvider_ShouldNotBeNil {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNotNil(helper.memoryUsageString);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProvider_ShouldNotBeEmpty {
- (void)test_memoryUsageString_withFailingSystemInfoProvider_ShouldNotBeEmpty {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertGreaterThan(helper.memoryUsageString.length, 0);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProvider_ShouldEvnaluateTo0 {
- (void)test_memoryUsageString_withFailingSystemInfoProvider_ShouldEvnaluateTo0 {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertEqual(helper.memoryUsageString.integerValue, 0);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProviderThatReturnsValue_DoesNotThrow {
- (void)test_memoryUsageString_withFailingSystemInfoProviderThatReturnsValue_DoesNotThrow {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithResidentSize:&residentSize error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNoThrow(helper.memoryUsageString);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProviderThatReturnsValue_ShouldNotBeNil {
- (void)test_memoryUsageString_withFailingSystemInfoProviderThatReturnsValue_ShouldNotBeNil {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithResidentSize:&residentSize error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNotNil(helper.memoryUsageString);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProviderThatReturnsValue_ShouldNotBeEmpty {
- (void)test_memoryUsageString_withFailingSystemInfoProviderThatReturnsValue_ShouldNotBeEmpty {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithResidentSize:&residentSize error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertGreaterThan(helper.memoryUsageString.length, 0);
}

- (void)test_MemoryUsageString_withFailingSystemInfoProviderThatReturnsValue_ShouldEvnaluateTo0 {
- (void)test_memoryUsageString_withFailingSystemInfoProviderThatReturnsValue_ShouldEvnaluateTo0 {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithResidentSize:&residentSize error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertEqual(helper.memoryUsageString.integerValue, 0);
}

- (void)test_MemoryUsageString_withSucceedingSystemInfoProvider_DoesNotThrow {
- (void)test_memoryUsageString_withSucceedingSystemInfoProvider_DoesNotThrow {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithResidentSize:&residentSize];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNoThrow(helper.memoryUsageString);
}

- (void)test_MemoryUsageString_withSucceedingSystemInfoProvider_ShouldNotBeNil {
- (void)test_memoryUsageString_withSucceedingSystemInfoProvider_ShouldNotBeNil {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithResidentSize:&residentSize];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNotNil(helper.memoryUsageString);
}

- (void)test_MemoryUsageString_withSucceedingSystemInfoProvider_ShouldNotBeEmpty {
- (void)test_memoryUsageString_withSucceedingSystemInfoProvider_ShouldNotBeEmpty {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithResidentSize:&residentSize];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertGreaterThan(helper.memoryUsageString.length, 0);
}

- (void)test_MemoryUsageString_withSucceedingSystemInfoProvider_ShouldReturnFormattedProvidedValue {
- (void)test_memoryUsageString_withSucceedingSystemInfoProvider_ShouldReturnFormattedProvidedValue {
vm_size_t residentSize = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithResidentSize:&residentSize];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];
Expand All @@ -213,6 +227,128 @@ - (void)test_MemoryUsageString_withSucceedingSystemInfoProvider_ShouldReturnForm
XCTAssertEqualObjects(helper.memoryUsageString, expectedString);
}

#pragma mark - memoryPhysicalFootprint

- (void)test_memoryPhysicalFootprint_withFailingSystemInfoProvider_ShouldBeZero {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertEqual(helper.memoryPhysicalFootprint, 0);
}

- (void)test_memoryPhysicalFootprint_withFailingSystemInfoProviderThatReturnsValue_ShouldBeZero {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithPhysFootprint:&physFootprint error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertEqual(helper.memoryPhysicalFootprint, 0);
}

- (void)test_memoryPhysicalFootprint_withSucceedingSystemInfoProvider_ShouldReturnProvidedValue {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithPhysFootprint:&physFootprint];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertEqual(helper.memoryPhysicalFootprint, physFootprint);
}

#pragma mark - memoryPhysicalFootprintString

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProvider_DoesNotThrow {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNoThrow(helper.memoryPhysicalFootprintString);
}

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProvider_ShouldNotBeNil {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNotNil(helper.memoryPhysicalFootprintString);
}

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProvider_ShouldNotBeEmpty {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertGreaterThan(helper.memoryPhysicalFootprintString.length, 0);
}

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProvider_ShouldEvnaluateTo0 {
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithError:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertEqual(helper.memoryPhysicalFootprintString.integerValue, 0);
}

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProviderThatReturnsValue_DoesNotThrow {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithPhysFootprint:&physFootprint error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNoThrow(helper.memoryPhysicalFootprintString);
}

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProviderThatReturnsValue_ShouldNotBeNil {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithPhysFootprint:&physFootprint error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNotNil(helper.memoryPhysicalFootprintString);
}

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProviderThatReturnsValue_ShouldNotBeEmpty {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithPhysFootprint:&physFootprint error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertGreaterThan(helper.memoryPhysicalFootprintString.length, 0);
}

- (void)test_memoryPhysicalFootprintString_withFailingSystemInfoProviderThatReturnsValue_ShouldEvnaluateTo0 {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock failingProviderWithPhysFootprint:&physFootprint error:nil];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertEqual(helper.memoryPhysicalFootprintString.integerValue, 0);
}

- (void)test_memoryPhysicalFootprintString_withSucceedingSystemInfoProvider_DoesNotThrow {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithPhysFootprint:&physFootprint];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNoThrow(helper.memoryPhysicalFootprintString);
}

- (void)test_memoryPhysicalFootprintString_withSucceedingSystemInfoProvider_ShouldNotBeNil {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithPhysFootprint:&physFootprint];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertNotNil(helper.memoryPhysicalFootprintString);
}

- (void)test_memoryPhysicalFootprintString_withSucceedingSystemInfoProvider_ShouldNotBeEmpty {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithPhysFootprint:&physFootprint];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];

XCTAssertGreaterThan(helper.memoryPhysicalFootprintString.length, 0);
}

- (void)test_memoryPhysicalFootprintString_withSucceedingSystemInfoProvider_ShouldReturnFormattedProvidedValue {
vm_size_t physFootprint = arc4random();
CSSystemInfoProviderMock *provider = [CSSystemInfoProviderMock succeedingProviderWithPhysFootprint:&physFootprint];
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:provider];
NSString *expectedString = [helper formatByteCount:physFootprint];

XCTAssertEqualObjects(helper.memoryPhysicalFootprintString, expectedString);
}

#pragma mark - platformUUID

#if TARGET_OS_OSX
- (void)test_PlatformUUID_DoesNotThrow {
CSSystemInfoHelper *helper = [[CSSystemInfoHelper alloc] initWithSystemInfoProvider:nil];
Expand Down
2 changes: 2 additions & 0 deletions CSSystemInfoHelperTests/CSSystemInfoProviderMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ NS_ASSUME_NONNULL_BEGIN

+ (instancetype)failingProviderWithError:(NSError *_Nullable)error;
+ (instancetype)failingProviderWithResidentSize:(vm_size_t * _Nullable)residentSize error:(NSError *_Nullable)error;
+ (instancetype)failingProviderWithPhysFootprint:(vm_size_t * _Nullable)physFootprint error:(NSError *_Nullable)error;

+ (instancetype)succeedingProviderWithNetworkInterfaces:(NSArray<CSNetworkInterface *> *)networkInterfaces;
+ (instancetype)succeedingProviderWithResidentSize:(vm_size_t *)residentSize;
+ (instancetype)succeedingProviderWithPhysFootprint:(vm_size_t *)physFootprint;

@end

Expand Down
32 changes: 32 additions & 0 deletions CSSystemInfoHelperTests/CSSystemInfoProviderMock.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @interface CSSystemInfoProviderMock ()
@property NSError *error;
@property NSArray<CSNetworkInterface *> *networkInterfaces;
@property vm_size_t *residentSize;
@property vm_size_t *physFootprint;

@end

Expand All @@ -34,6 +35,15 @@ + (instancetype)failingProviderWithResidentSize:(vm_size_t *)residentSize error:
return provider;
}

+ (instancetype)failingProviderWithPhysFootprint:(vm_size_t *)physFootprint error:(NSError *)error {
CSSystemInfoProviderMock * provider = [CSSystemInfoProviderMock new];
provider.shouldSucceed = NO;
provider.error = error;
provider.physFootprint = physFootprint;
return provider;

}

+ (instancetype)succeedingProviderWithNetworkInterfaces:(NSArray<CSNetworkInterface *> *)networkInterfaces {
CSSystemInfoProviderMock * provider = [CSSystemInfoProviderMock new];
provider.shouldSucceed = YES;
Expand All @@ -48,6 +58,13 @@ + (instancetype)succeedingProviderWithResidentSize:(vm_size_t *)residentSize {
return provider;
}

+ (instancetype)succeedingProviderWithPhysFootprint:(vm_size_t *)physFootprint {
CSSystemInfoProviderMock * provider = [CSSystemInfoProviderMock new];
provider.shouldSucceed = YES;
provider.physFootprint = physFootprint;
return provider;
}

#pragma mark - CSSystemInfoProviderProtocol

- (nullable NSArray<CSNetworkInterface *> *)queryNetworkInterfaces:(NSError *__autoreleasing _Nullable * _Nullable)error {
Expand Down Expand Up @@ -76,5 +93,20 @@ - (BOOL)getResidentSize:(nonnull vm_size_t *)residentSize error:(NSError *__auto
return YES;
}

-(BOOL)getPhysFootprint:(vm_size_t *)physFootprint error:(NSError *__autoreleasing _Nullable *)error {
if (self.physFootprint) {
*physFootprint = *(self.physFootprint);
}

if (!self.shouldSucceed) {
if (error) {
*error = self.error;
}
return NO;
}

return YES;
}


@end

0 comments on commit 2b0bcd4

Please sign in to comment.