File: test_code_template.cpp

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 (62 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download | duplicates (3)
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
#include <gtest/gtest.h>

#include <ATen/code_template.h>
#include <test/cpp/jit/test_utils.h>

namespace torch {
namespace jit {

static const auto ct = at::jit::CodeTemplate(R"(
  int foo($args) {

      $bar
          $bar
      $a+$b
  }
  int commatest(int a${,stuff})
  int notest(int a${,empty,})
  )");
static const auto ct_expect = R"(
  int foo(hi, 8) {

      what
      on many
      lines...
      7
          what
          on many
          lines...
          7
      3+4
  }
  int commatest(int a, things..., others)
  int notest(int a)
  )";

TEST(TestCodeTemplate, Copying) {
  at::jit::TemplateEnv e;
  e.s("hi", "foo");
  e.v("what", {"is", "this"});
  at::jit::TemplateEnv c(e);
  c.s("hi", "foo2");
  ASSERT_EQ(e.s("hi"), "foo");
  ASSERT_EQ(c.s("hi"), "foo2");
  ASSERT_EQ(e.v("what")[0], "is");
}

TEST(TestCodeTemplate, Formatting) {
  at::jit::TemplateEnv e;
  e.v("args", {"hi", "8"});
  e.v("bar", {"what\non many\nlines...", "7"});
  e.s("a", "3");
  e.s("b", "4");
  e.v("stuff", {"things...", "others"});
  e.v("empty", {});
  auto s = ct.format(e);
  // std::cout << "'" << s << "'\n";
  // std::cout << "'" << ct_expect << "'\n";
  ASSERT_EQ(s, ct_expect);
}

} // namespace jit
} // namespace torch