File size: 1,421 Bytes
67e1d01
 
51c8a9f
67e1d01
51c8a9f
 
 
 
 
67e1d01
51c8a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
67e1d01
 
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
import unittest
from neuro_compiler import NeuralCompiler
from neuro_compiler.neurocc_graph_live import raw_bytes

class Contract(unittest.TestCase):
    def test_invalid_sources(self):
        for source in ('', 'int f(){return 1;}', 'unsigned f(unsigned a,unsigned b,unsigned c){return (a/2u);}', 'unsigned f(unsigned a,unsigned b,unsigned c){return (a+64u);}', '\x00', 'λ'):
            with self.assertRaises(ValueError): NeuralCompiler._inputs([source])
        with self.assertRaises(TypeError): NeuralCompiler._inputs('source')

    def test_byte_contract(self):
        for row in ([195,257,257],[195,256],[257],[195,257,0],[999,257]): self.assertIsNone(raw_bytes(row))
        self.assertEqual(raw_bytes([195,257,256]),b'\xc3')

    def test_missing_and_corrupted_weights(self):
        from pathlib import Path
        import tempfile
        import shutil
        source=Path(__file__).resolve().parents[1]/'manifest.json'
        with tempfile.TemporaryDirectory() as folder:
            root=Path(folder); shutil.copy2(source,root/'manifest.json')
            with self.assertRaises(FileNotFoundError): NeuralCompiler.from_pretrained(root,device='cpu')
            (root/'weights').mkdir(); (root/'weights/ast.safetensors').write_bytes(b'corrupted')
            with self.assertRaisesRegex(ValueError,'integrity'): NeuralCompiler.from_pretrained(root,device='cpu')

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