File size: 1,595 Bytes
13065f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest

import numpy as np
import torch

from hikka_forge import ForgeItem, ForgeVector
from hikka_forge.api import Forge2Vec


class PublicApiTests(unittest.TestCase):
    def test_legacy_field_names_are_normalized(self):
        item = ForgeItem.from_value({
            "en_title": "Frieren",
            "original_title": "Sousou no Frieren",
            "alternate_names": ["Frieren at the Funeral"],
            "en_description": "A fantasy journey.",
            "type": "anime",
        })
        self.assertEqual(item.title, "Frieren")
        self.assertEqual(item.native_title, "Sousou no Frieren")
        self.assertEqual(item.synonyms, ["Frieren at the Funeral"])

    def test_vector_arithmetic_preserves_shape(self):
        a = ForgeVector(np.ones(256, dtype=np.float32))
        b = ForgeVector(np.full(256, 2.0, dtype=np.float32))
        result = a - b + 0.5 * b
        self.assertEqual(result.shape, (256,))
        np.testing.assert_allclose(result.numpy(), np.zeros(256), atol=1e-6)

    def test_normalized_vector_has_unit_norm(self):
        vector = ForgeVector(np.arange(1, 257, dtype=np.float32)).normalized()
        self.assertAlmostEqual(float(np.linalg.norm(vector.numpy())), 1.0, places=6)

    def test_tensor_poster_is_resized_and_normalized(self):
        poster = torch.full((480, 320, 3), 255, dtype=torch.uint8)
        pixels = Forge2Vec._poster_tensor(poster)
        self.assertEqual(tuple(pixels.shape), (3, 224, 224))
        self.assertTrue(torch.allclose(pixels, torch.ones_like(pixels)))


if __name__ == "__main__":
    unittest.main()