File size: 3,637 Bytes
5cdf637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors

#include <unistd.h>

#include <filesystem>
#include <fstream>
#include <string>
#include <vector>

#include "gtest/gtest.h"
#include "odb/db.h"
#include "odb/geom.h"
#include "tst/nangate45_fixture.h"
#include "web/web.h"

namespace web {
namespace {

class GifTest : public tst::Nangate45Fixture
{
 protected:
  void SetUp() override
  {
    block_->setDieArea(odb::Rect(0, 0, 20000, 20000));
    for (int i = 0; i < 8; ++i) {
      odb::dbMaster* master = lib_->findMaster("BUF_X16");
      ASSERT_NE(master, nullptr);
      odb::dbInst* inst = odb::dbInst::create(
          block_, master, ("buf" + std::to_string(i)).c_str());
      inst->setLocation(2000 + i * 1800, 2000 + i * 1800);
      inst->setPlacementStatus(odb::dbPlacementStatus::PLACED);
    }
  }

  void TearDown() override
  {
    for (const auto& path : output_files_) {
      std::filesystem::remove(path);
    }
  }

  std::string tempGif(const std::string& label)
  {
    std::string path
        = std::filesystem::temp_directory_path()
          / ("web_gif_" + label + "_" + std::to_string(::getpid()) + ".gif");
    output_files_.push_back(path);
    return path;
  }

  // GIF files start with the 6-byte "GIF89a" signature.
  static bool hasGifSignature(const std::string& path)
  {
    std::ifstream f(path, std::ios::binary);
    char sig[6] = {0};
    f.read(sig, 6);
    return f.gcount() == 6 && std::string(sig, 6) == "GIF89a";
  }

  WebServer makeServer()
  {
    return WebServer(getDb(), nullptr, getLogger(), nullptr);
  }

  std::vector<std::string> output_files_;
};

TEST_F(GifTest, StartAddEndProducesValidGif)
{
  const std::string path = tempGif("basic");
  WebServer server = makeServer();

  const int key = server.gifStart(path);
  EXPECT_EQ(key, 0);
  const odb::Rect area(0, 0, 0, 0);  // zero → whole die + margin
  server.gifAddFrame(key, area, /*width_px=*/200, 0, /*delay=*/5, "");
  server.gifAddFrame(key, area, /*width_px=*/200, 0, /*delay=*/5, "");
  server.gifEnd(key);

  ASSERT_TRUE(std::filesystem::exists(path));
  EXPECT_GT(std::filesystem::file_size(path), 0u);
  EXPECT_TRUE(hasGifSignature(path));
}

TEST_F(GifTest, LaterFramesWithDifferentSizeAreScaled)
{
  const std::string path = tempGif("scaled");
  WebServer server = makeServer();

  const int key = server.gifStart(path);
  const odb::Rect area(0, 0, 0, 0);
  server.gifAddFrame(key, area, /*width_px=*/200, 0, 5, "");
  // Different width → must be resampled to the first frame's dimensions.
  server.gifAddFrame(key, area, /*width_px=*/400, 0, 5, "");
  server.gifEnd(key);

  ASSERT_TRUE(std::filesystem::exists(path));
  EXPECT_TRUE(hasGifSignature(path));
}

TEST_F(GifTest, EndWithoutFramesWritesNoFile)
{
  const std::string path = tempGif("empty");
  WebServer server = makeServer();

  const int key = server.gifStart(path);
  server.gifEnd(key);  // no frames added

  // The encoder only opens the file on the first frame, so nothing is written.
  EXPECT_FALSE(std::filesystem::exists(path));
}

TEST_F(GifTest, MultipleConcurrentStreamsGetDistinctKeys)
{
  WebServer server = makeServer();
  const std::string a = tempGif("streamA");
  const std::string b = tempGif("streamB");

  const int ka = server.gifStart(a);
  const int kb = server.gifStart(b);
  EXPECT_NE(ka, kb);

  const odb::Rect area(0, 0, 0, 0);
  server.gifAddFrame(ka, area, 150, 0, 5, "");
  server.gifAddFrame(kb, area, 150, 0, 5, "");
  server.gifEnd(ka);
  server.gifEnd(kb);

  EXPECT_TRUE(hasGifSignature(a));
  EXPECT_TRUE(hasGifSignature(b));
}

}  // namespace
}  // namespace web