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