| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/os.h" |
| 5 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 6 #include "platform/utils.h" | 7 #include "platform/utils.h" |
| 7 #include "vm/globals.h" | 8 #include "vm/globals.h" |
| 8 #include "vm/os.h" | |
| 9 #include "vm/unit_test.h" | 9 #include "vm/unit_test.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 VM_UNIT_TEST_CASE(Sleep) { | 13 VM_UNIT_TEST_CASE(Sleep) { |
| 14 // All times measured in microseconds. | 14 // All times measured in microseconds. |
| 15 int64_t start_time = OS::GetCurrentMonotonicMicros(); | 15 int64_t start_time = OS::GetCurrentMonotonicMicros(); |
| 16 int64_t sleep_time = 702000; | 16 int64_t sleep_time = 702000; |
| 17 OS::SleepMicros(sleep_time); | 17 OS::SleepMicros(sleep_time); |
| 18 int64_t delta = OS::GetCurrentMonotonicMicros() - start_time; | 18 int64_t delta = OS::GetCurrentMonotonicMicros() - start_time; |
| 19 const int kAcceptableSleepWakeupJitter = 200000; | 19 const int kAcceptableSleepWakeupJitter = 200000; |
| 20 EXPECT_GE(delta, sleep_time - kAcceptableSleepWakeupJitter); | 20 EXPECT_GE(delta, sleep_time - kAcceptableSleepWakeupJitter); |
| 21 EXPECT_LE(delta, sleep_time + kAcceptableSleepWakeupJitter); | 21 EXPECT_LE(delta, sleep_time + kAcceptableSleepWakeupJitter); |
| 22 } | 22 } |
| 23 | 23 |
| 24 | |
| 25 VM_UNIT_TEST_CASE(SNPrint) { | 24 VM_UNIT_TEST_CASE(SNPrint) { |
| 26 char buffer[256]; | 25 char buffer[256]; |
| 27 int length; | 26 int length; |
| 28 length = OS::SNPrint(buffer, 10, "%s", "foo"); | 27 length = OS::SNPrint(buffer, 10, "%s", "foo"); |
| 29 EXPECT_EQ(3, length); | 28 EXPECT_EQ(3, length); |
| 30 EXPECT_STREQ("foo", buffer); | 29 EXPECT_STREQ("foo", buffer); |
| 31 length = OS::SNPrint(buffer, 3, "%s", "foo"); | 30 length = OS::SNPrint(buffer, 3, "%s", "foo"); |
| 32 EXPECT_EQ(3, length); | 31 EXPECT_EQ(3, length); |
| 33 EXPECT_STREQ("fo", buffer); | 32 EXPECT_STREQ("fo", buffer); |
| 34 length = OS::SNPrint(buffer, 256, "%s%c%d", "foo", 'Z', 42); | 33 length = OS::SNPrint(buffer, 256, "%s%c%d", "foo", 'Z', 42); |
| 35 EXPECT_EQ(6, length); | 34 EXPECT_EQ(6, length); |
| 36 EXPECT_STREQ("fooZ42", buffer); | 35 EXPECT_STREQ("fooZ42", buffer); |
| 37 length = OS::SNPrint(NULL, 0, "foo"); | 36 length = OS::SNPrint(NULL, 0, "foo"); |
| 38 EXPECT_EQ(3, length); | 37 EXPECT_EQ(3, length); |
| 39 } | 38 } |
| 40 | 39 |
| 41 | |
| 42 // This test is expected to crash when it runs. | 40 // This test is expected to crash when it runs. |
| 43 VM_UNIT_TEST_CASE(SNPrint_BadArgs) { | 41 VM_UNIT_TEST_CASE(SNPrint_BadArgs) { |
| 44 int width = kMaxInt32; | 42 int width = kMaxInt32; |
| 45 int num = 7; | 43 int num = 7; |
| 46 OS::SNPrint(NULL, 0, "%*d%*d", width, num, width, num); | 44 OS::SNPrint(NULL, 0, "%*d%*d", width, num, width, num); |
| 47 } | 45 } |
| 48 | 46 |
| 49 | |
| 50 VM_UNIT_TEST_CASE(OsFuncs) { | 47 VM_UNIT_TEST_CASE(OsFuncs) { |
| 51 EXPECT(Utils::IsPowerOfTwo(OS::ActivationFrameAlignment())); | 48 EXPECT(Utils::IsPowerOfTwo(OS::ActivationFrameAlignment())); |
| 52 EXPECT(Utils::IsPowerOfTwo(OS::PreferredCodeAlignment())); | 49 EXPECT(Utils::IsPowerOfTwo(OS::PreferredCodeAlignment())); |
| 53 int procs = OS::NumberOfAvailableProcessors(); | 50 int procs = OS::NumberOfAvailableProcessors(); |
| 54 EXPECT_LE(1, procs); | 51 EXPECT_LE(1, procs); |
| 55 } | 52 } |
| 56 | 53 |
| 57 } // namespace dart | 54 } // namespace dart |
| OLD | NEW |