File: common_test.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (43 lines) | stat: -rw-r--r-- 1,110 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
#include <iostream>
#include <memory>

#define CAFFE2_TESTONLY_FORCE_STD_STRING_TEST

#include "caffe2/core/common.h"
#include <gtest/gtest.h>

namespace caffe2 {

#ifndef __ANDROID__

// Simple tests to make sure that our stoi and stod implementations are
// matching the std implementations, but not testing it very extensively
// as one should be using the std version most of the time.
TEST(CommonTest, TestStoi) {
  EXPECT_TRUE(CAFFE2_TESTONLY_WE_ARE_USING_CUSTOM_STRING_FUNCTIONS);
  string s = "1234";
  int i_std = std::stoi(s);
  int i_caffe2 = ::c10::stoi(s);
  EXPECT_EQ(i_std, i_caffe2);
}

TEST(CommonTest, TestStod) {
  // Full string is parsed.
  string s = "1.234";
  std::size_t p_std = 0, p_caffe2 = 0;
  double d_std = std::stod(s, &p_std);
  double d_caffe2 = ::c10::stod(s, &p_caffe2);
  EXPECT_EQ(d_std, d_caffe2);
  EXPECT_EQ(p_std, p_caffe2);

  // Only part of the string is parsed.
  s = "1.234 5.678";
  d_std = std::stod(s, &p_std);
  d_caffe2 = ::c10::stod(s, &p_caffe2);
  EXPECT_EQ(d_std, d_caffe2);
  EXPECT_EQ(p_std, p_caffe2);
}

#endif // __ANDROID__

}  // namespace caffe2