Skip to content

Commit

Permalink
Merge pull request #28 from b52/progress-overflow
Browse files Browse the repository at this point in the history
Progress overflow
  • Loading branch information
aomader committed Nov 23, 2015
2 parents e36b0ef + 782cf6d commit 031a644
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Classes/Core/OMDeferred.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// OMPromise.h
// OMPromises
//
// Copyright (C) 2013,2014 Oliver Mader
// Copyright (C) 2013-2015 Oliver Mader
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -76,7 +76,7 @@

/** Finalizes the deferred by settings its state to OMPromiseStateFulfilled.
Implicitly sets the progress to 1.
Implicitly sets the progress to 1.0f.
@param result Result to set and propagate.
@see fail:
Expand All @@ -93,7 +93,7 @@
/** Update the progress.
The new progress has to be higher than the previous one. Equal values are skipped,
but lower values raise an exception.
but lower values raise an exception. The progress must be less than or equal to 1.0f.
@param progress Higher progress to set and propagate.
*/
Expand Down
5 changes: 3 additions & 2 deletions Classes/Core/OMDeferred.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// OMPromise.h
// OMPromises
//
// Copyright (C) 2013,2014 Oliver Mader
// Copyright (C) 2013-2015 Oliver Mader
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -88,9 +88,10 @@ - (void)progress:(float)progress {
@synchronized (self) {
NSAssert(self.state == OMPromiseStateUnfulfilled, @"Can only progress while being Unfulfilled");
NSAssert(self.progress <= progress + FLT_EPSILON, @"Progress must not decrease");
NSAssert(progress <= 1.0f + FLT_EPSILON, @"Progress must be in range (0, 1]");

if (self.progress < progress - FLT_EPSILON) {
self.progress = progress;
self.progress = MIN(1.0f, progress);
progressHandlers = self.progressHandlers;
}

Expand Down
4 changes: 2 additions & 2 deletions Classes/HTTP/OMHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];

if (self.expectedContentLength > 0) {
[self progress:self.lookup + (1 - self.lookup) *
(float)self.data.length / self.expectedContentLength];
[self progress:MIN(1.0f, self.lookup + (1 - self.lookup) *
(float)self.data.length / self.expectedContentLength)];
}
}

Expand Down
7 changes: 6 additions & 1 deletion Tests/Core/OMDeferredTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ - (void)testProgress {

XCTAssertThrows([deferred progress:.1f], @"Must not decrease progress");
XCTAssertEqualWithAccuracy(deferred.progress, .2f, FLT_EPSILON, @"Progress shouldn't have changed");

XCTAssertThrows([deferred progress:1.1f], @"Progress must not exceed 1.0f");
}

- (void)testProgressPrecision {
OMDeferred *deferred = [OMDeferred deferred];

__block int called = 0;
[deferred.promise progressed:^(float progress) {
float values[] = {.1f, .1 + 2.f*FLT_EPSILON};
float values[] = {.1f, .1 + 2.f*FLT_EPSILON, 1.0f};
XCTAssertEqualWithAccuracy(values[called], progress, FLT_EPSILON, @"Unexpected progress");
called += 1;
}];
Expand All @@ -113,6 +115,9 @@ - (void)testProgressPrecision {

[deferred progress:.1f + 2.f*FLT_EPSILON];
XCTAssertEqual(called, 2, @"Finally we progress if the change is large enough");

[deferred progress:1.0f + FLT_EPSILON];
XCTAssertEqual(deferred.progress, 1.0f);
}

- (void)testTryFulfil {
Expand Down

0 comments on commit 031a644

Please sign in to comment.