| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "bin/file.h" | 5 #include "bin/file.h" |
| 6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
| 8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
| 9 | 9 |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 namespace bin { | 12 namespace bin { |
| 13 | 13 |
| 14 // Helper method to be able to run the test from the runtime | 14 // Helper method to be able to run the test from the runtime |
| 15 // directory, or the top directory. | 15 // directory, or the top directory. |
| 16 static const char* GetFileName(const char* name) { | 16 static const char* GetFileName(const char* name) { |
| 17 if (File::Exists(name)) { | 17 if (File::Exists(name)) { |
| 18 return name; | 18 return name; |
| 19 } else { | 19 } else { |
| 20 static const int kRuntimeLength = strlen("runtime/"); | 20 static const int kRuntimeLength = strlen("runtime/"); |
| 21 return name + kRuntimeLength; | 21 return name + kRuntimeLength; |
| 22 } | 22 } |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 UNIT_TEST_CASE(Read) { | 26 TEST_CASE(Read) { |
| 27 const char* kFilename = GetFileName("runtime/bin/file_test.cc"); | 27 const char* kFilename = GetFileName("runtime/bin/file_test.cc"); |
| 28 File* file = File::Open(kFilename, File::kRead); | 28 File* file = File::Open(kFilename, File::kRead); |
| 29 EXPECT(file != NULL); | 29 EXPECT(file != NULL); |
| 30 char buffer[16]; | 30 char buffer[16]; |
| 31 buffer[0] = '\0'; | 31 buffer[0] = '\0'; |
| 32 EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true. | 32 EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true. |
| 33 buffer[13] = '\0'; | 33 buffer[13] = '\0'; |
| 34 EXPECT_STREQ("// Copyright ", buffer); | 34 EXPECT_STREQ("// Copyright ", buffer); |
| 35 EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file. | 35 EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file. |
| 36 delete file; | 36 delete file; |
| 37 } | 37 } |
| 38 | 38 |
| 39 | 39 |
| 40 UNIT_TEST_CASE(FileLength) { | 40 TEST_CASE(FileLength) { |
| 41 const char* kFilename = | 41 const char* kFilename = |
| 42 GetFileName("runtime/tests/vm/data/fixed_length_file"); | 42 GetFileName("runtime/tests/vm/data/fixed_length_file"); |
| 43 File* file = File::Open(kFilename, File::kRead); | 43 File* file = File::Open(kFilename, File::kRead); |
| 44 EXPECT(file != NULL); | 44 EXPECT(file != NULL); |
| 45 EXPECT_EQ(42, file->Length()); | 45 EXPECT_EQ(42, file->Length()); |
| 46 delete file; | 46 delete file; |
| 47 } | 47 } |
| 48 | 48 |
| 49 | 49 |
| 50 UNIT_TEST_CASE(FilePosition) { | 50 TEST_CASE(FilePosition) { |
| 51 char buf[42]; | 51 char buf[42]; |
| 52 const char* kFilename = | 52 const char* kFilename = |
| 53 GetFileName("runtime/tests/vm/data/fixed_length_file"); | 53 GetFileName("runtime/tests/vm/data/fixed_length_file"); |
| 54 File* file = File::Open(kFilename, File::kRead); | 54 File* file = File::Open(kFilename, File::kRead); |
| 55 EXPECT(file != NULL); | 55 EXPECT(file != NULL); |
| 56 EXPECT(file->ReadFully(buf, 12)); | 56 EXPECT(file->ReadFully(buf, 12)); |
| 57 EXPECT_EQ(12, file->Position()); | 57 EXPECT_EQ(12, file->Position()); |
| 58 EXPECT(file->ReadFully(buf, 6)); | 58 EXPECT(file->ReadFully(buf, 6)); |
| 59 EXPECT_EQ(18, file->Position()); | 59 EXPECT_EQ(18, file->Position()); |
| 60 delete file; | 60 delete file; |
| 61 } | 61 } |
| 62 | 62 |
| 63 } // namespace bin | 63 } // namespace bin |
| 64 } // namespace dart | 64 } // namespace dart |
| OLD | NEW |