Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_source_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <gtest/gtest.h>
38
39# include <ah-source.H>
40
41# include <atomic>
42# include <chrono>
43# include <filesystem>
44# include <fstream>
45# include <string>
46#if defined(_WIN32)
47# include <process.h>
48#else
49# include <unistd.h>
50#endif
51
52using namespace Aleph;
53
54namespace
55{
57 long long process_id() noexcept
58 {
59#if defined(_WIN32)
60 return static_cast<long long>(_getpid());
61#else
62 return static_cast<long long>(getpid());
63#endif
64 }
65
66 std::filesystem::path
67 make_temp_file_path(const std::string & stem)
68 {
69 namespace fs = std::filesystem;
70 // A steady_clock tick alone is unique *within* a process, but not
71 // across the several processes that actually run this suite (each
72 // TEST() is its own ctest process, and CI runs ctest with
73 // --parallel) -- two processes' calls landing in the same clock
74 // tick produce the identical name and race on the same file.
75 // Mixing in the process id and a per-process counter closes that
76 // gap regardless of clock resolution or call count.
77 static std::atomic<unsigned long long> counter{0};
78 return fs::temp_directory_path() /
79 ("aleph_" + stem + "_" +
80 std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) +
81 "_" + std::to_string(process_id()) +
82 "_" + std::to_string(counter++) +
83 ".txt");
84 }
85}
86
87
89{
91 const auto id = sm.add_virtual_file("main.aw", "let x = 1;\n");
92
93 EXPECT_EQ(sm.num_files(), 1u);
94 EXPECT_EQ(sm.file_name(id), "main.aw");
95 EXPECT_EQ(sm.file_text(id), "let x = 1;\n");
96 EXPECT_EQ(sm.file_size(id), 11u);
97}
98
99
101{
103 const auto id = sm.add_virtual_file("main.aw", "ab\ncde\n");
104
105 const auto p0 = sm.position(id, 0);
106 EXPECT_EQ(p0.line, 1u);
107 EXPECT_EQ(p0.column, 1u);
108
109 const auto p2 = sm.position(id, 2);
110 EXPECT_EQ(p2.line, 1u);
111 EXPECT_EQ(p2.column, 3u);
112
113 const auto p3 = sm.position(id, 3);
114 EXPECT_EQ(p3.line, 2u);
115 EXPECT_EQ(p3.column, 1u);
116
117 const auto p7 = sm.position(id, 7);
118 EXPECT_EQ(p7.line, 3u);
119 EXPECT_EQ(p7.column, 1u);
120}
121
122
124{
126 const auto id = sm.add_virtual_file("main.aw", "alpha beta gamma");
127 const auto sp = sm.span(id, 6, 10);
128
129 EXPECT_FALSE(sp.empty());
130 EXPECT_EQ(sp.size(), 4u);
131 EXPECT_TRUE(sp.contains(7));
132 EXPECT_EQ(sm.slice(sp), "beta");
133}
134
135
137{
139 const auto id = sm.add_virtual_file("main.aw", "alpha beta gamma\n");
140 const auto sp = sm.span(id, 6, 10);
141 const auto snip = sm.snippet(sp);
142
143 EXPECT_EQ(snip.file_name, "main.aw");
144 EXPECT_EQ(snip.line, 1u);
145 EXPECT_EQ(snip.column, 7u);
146 EXPECT_EQ(snip.line_text, "alpha beta gamma");
147 EXPECT_EQ(snip.highlight_column, 7u);
148 EXPECT_EQ(snip.highlight_width, 4u);
149}
150
151
153{
155 const auto id = sm.add_virtual_file("main.aw", "abc");
156 const auto sp = sm.span(id, 1, 1);
157 const auto snip = sm.snippet(sp);
158
159 EXPECT_TRUE(sp.empty());
160 EXPECT_EQ(sm.slice(sp), "");
161 EXPECT_EQ(snip.highlight_column, 2u);
162 EXPECT_EQ(snip.highlight_width, 1u);
163}
164
165
167{
168 namespace fs = std::filesystem;
169
170 const fs::path path = make_temp_file_path("source_manager");
171 {
172 std::ofstream out(path, std::ios::binary);
173 out << "line1\nline2\n";
174 }
175
177 const auto id = sm.load_file(path.string());
178 EXPECT_EQ(sm.file_name(id), path.string());
179 EXPECT_EQ(sm.file_text(id), "line1\nline2\n");
180
181 fs::remove(path);
182}
183
184
186{
188 const auto id = sm.add_virtual_file("main.aw", "abc");
189
190 EXPECT_THROW((void) sm.position(id, 4), std::out_of_range);
191 EXPECT_THROW((void) sm.span(id, 1, 4), std::out_of_range);
192 EXPECT_THROW((void) sm.file(999), std::out_of_range);
193}
Source file and span management utilities for compiler-style tooling.
size_t size_t int32_t * out
Definition ca-c-api.h:120
Stores source files and resolves offsets into human-readable data.
Definition ah-source.H:184
Source_File_Id load_file(const std::string &path)
Loads a file from disk and registers it.
Definition ah-source.H:276
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Definition ah-source.H:258
#define TEST(name)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
static long counter
Definition test-splice.C:40