File: test_file_util.cpp

package info (click to toggle)
pytorch 2.9.1%2Bdfsg-1~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 180,096 kB
  • sloc: python: 1,473,255; cpp: 942,030; ansic: 79,796; asm: 7,754; javascript: 2,502; java: 1,962; sh: 1,809; makefile: 628; xml: 8
file content (111 lines) | stat: -rw-r--r-- 2,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <gtest/gtest.h>
#include <torch/nativert/common/FileUtil.h>
#include <fstream>

namespace torch {
namespace nativert {

TEST(FileUtilTest, OpenNoInt) {
  // Create a temporary file
  std::ofstream tmpFile("tmp_file.txt");
  tmpFile.close();

  int fd = openNoInt("tmp_file.txt", O_RDONLY, 0);
  ASSERT_GE(fd, 0);

  closeNoInt(fd);
}

TEST(FileUtilTest, CloseNoInt) {
  // Create a temporary file
  std::ofstream tmpFile("tmp_file.txt");
  tmpFile.close();

  int fd = openNoInt("tmp_file.txt", O_RDONLY, 0);
  ASSERT_GE(fd, 0);

  int result = closeNoInt(fd);
  ASSERT_EQ(result, 0);
}

TEST(FileUtilTest, WriteFull) {
  // Create a temporary file
  std::ofstream tmpFile("tmp_file.txt");
  tmpFile.close();

  int fd = openNoInt("tmp_file.txt", O_WRONLY | O_CREAT, 0644);
  ASSERT_GE(fd, 0);

  const char* data = "Hello, World!";
  ssize_t bytesWritten = writeFull(fd, data, strlen(data));
  ASSERT_EQ(bytesWritten, strlen(data));

  closeNoInt(fd);
}

TEST(FileUtilTest, ReadFull) {
  // Create a temporary file
  std::ofstream tmpFile("tmp_file.txt");
  tmpFile << "Hello, World!";
  tmpFile.close();

  int fd = openNoInt("tmp_file.txt", O_RDONLY, 0);
  ASSERT_GE(fd, 0);

  char buffer[1024];
  ssize_t bytesRead = readFull(fd, buffer, 1024);
  ASSERT_EQ(bytesRead, 13); // length of "Hello, World!"

  closeNoInt(fd);
}

TEST(FileUtilTest, FileConstructor) {
  // Create a temporary file
  std::ofstream tmpFile("tmp_file.txt");
  tmpFile.close();

  File file("tmp_file.txt", O_RDONLY, 0);
  ASSERT_GE(file.fd(), 0);

  file.close();
}

TEST(FileUtilTest, FileMoveConstructor) {
  // Create a temporary file
  std::ofstream tmpFile("tmp_file.txt");
  tmpFile.close();

  File file1("tmp_file.txt", O_RDONLY, 0);
  File file2(std::move(file1));

  ASSERT_GE(file2.fd(), 0);
  ASSERT_EQ(file1.fd(), -1);

  file2.close();
}

TEST(FileUtilTest, FileAssignmentOperator) {
  // Create a temporary file
  std::ofstream tmpFile("tmp_file.txt");
  tmpFile.close();

  File file1("tmp_file.txt", O_RDONLY, 0);
  File file2;

  file2 = std::move(file1);

  ASSERT_GE(file2.fd(), 0);
  ASSERT_EQ(file1.fd(), -1);

  file2.close();
}

TEST(FileUtilTest, TemporaryFile) {
  File file = File::temporary();
  ASSERT_GE(file.fd(), 0);

  file.close();
}

} // namespace nativert
} // namespace torch