File: test_lammps_shell.py

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (151 lines) | stat: -rw-r--r-- 5,735 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

import os, re, subprocess, unittest

# enable test mode
os.putenv('LAMMPS_SHELL_TESTING','1')

shell_prompt_re = r"([^>]*LAMMPS Shell> ([a-z0-9_]+) *([a-z0-9_\.]+)?.*\n)+"
cmd_group_re = r"([^>]*LAMMPS Shell> ([a-z0-9_]+) +([a-z0-9]+) +([a-z0-9]+)? *([a-z/0-9]+)?.*\n)+"

#
class LammpsShell(unittest.TestCase):

    def setUp(self):
        self.proc = subprocess.Popen('./lammps-shell',
                                     stdin=subprocess.PIPE, stdout=subprocess.PIPE)


    def tearDown(self):
        self.proc.kill()


    def InputRunner(self,text):
        """Test tab expansions"""
        try:
            [outs,errs] = self.proc.communicate(input=text, timeout=10)
            self.timeout = 0
        except subprocess.TimeoutExpired:
            self.proc.kill()
            [outs,errs] = self.proc.communicate()
            self.timeout = 1

        return outs.decode('UTF-8')

    def testExpandClearHistory(self):
        """Test expansion of a shell specific command"""
        matches = re.findall(shell_prompt_re, self.InputRunner(b'clear_his\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"clear_history")

    def testExpandDimension(self):
        """Test expansion of a LAMMPS command"""
        matches = re.findall(shell_prompt_re, self.InputRunner(b'dimens\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"dimension")

    def testExpandPairStyle(self):
        """Test expansion of a pair style"""
        matches = re.findall(shell_prompt_re, self.InputRunner(b'pair_st\t zer\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"pair_style")
            self.assertEqual(matches[0][2],"zero")

    def testExpandBondStyle(self):
        """Test expansion of a bond style"""
        matches = re.findall(shell_prompt_re, self.InputRunner(b'bond_st\t zer\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"bond_style")
            self.assertEqual(matches[0][2],"zero")

    def testExpandAngleStyle(self):
        """Test expansion of a angle style"""
        matches = re.findall(shell_prompt_re, self.InputRunner(b'angle_st\t zer\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"angle_style")
            self.assertEqual(matches[0][2],"zero")

    def testExpandDihedralStyle(self):
        """Test expansion of a dihedral style"""
        matches = re.findall(shell_prompt_re, self.InputRunner(b'dihedral_st\t zer\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"dihedral_style")
            self.assertEqual(matches[0][2],"zero")

    def testExpandImproperStyle(self):
        """Test expansion of a improper style"""
        matches = re.findall(shell_prompt_re, self.InputRunner(b'improper_st\t zer\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"improper_style")
            self.assertEqual(matches[0][2],"zero")

    def testExpandComputeGroup(self):
        """Test expansion of a group-ID and a compute command"""
        matches = re.findall(cmd_group_re, self.InputRunner(b'compute test al\tstress/at\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"compute")
            self.assertEqual(matches[0][2],"test")
            self.assertEqual(matches[0][3],"all")
            self.assertEqual(matches[0][4],"stress/atom")

    def testExpandFixGroup(self):
        """Test expansion of a group-ID and a fix command"""
        matches = re.findall(cmd_group_re, self.InputRunner(b'fix test al\tpropert\t\n'), re.MULTILINE)
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"fix")
            self.assertEqual(matches[0][2],"test")
            self.assertEqual(matches[0][3],"all")
            self.assertEqual(matches[0][4],"property/atom")

    def testExpandSource(self):
        """Test expansion of a shell command and a file name"""
        with open('.tmp.in.source', 'w') as out:
           print('units real', file=out)
        out.close()
        matches = re.findall(shell_prompt_re, self.InputRunner(b'sour\t.tmp.in.sou\t\n'), re.MULTILINE)
        os.remove('.tmp.in.source')
        if self.timeout:
            self.fail("Timeout")
        else:
            self.assertEqual(matches[0][1],"source")
            self.assertEqual(matches[0][2],".tmp.in.source")

    def testHistory(self):
        """Test history expansion"""
        out = self.InputRunner(b'clear_history\nunits real\ndimension 2\n!!:p\n!-3:p\n!dim:p\n!uni:p\nprint !!:$\nprint !dim:1\n')
        idx = 0
        if self.timeout:
            self.fail("Timeout")
        else:
            lines = out.splitlines()
            for line in lines:
                if line.startswith('LAMMPS Shell>'): break
                idx += 1
                
            self.assertEqual(lines[idx+4],"dimension 2")
            self.assertEqual(lines[idx+6],"units real")
            self.assertEqual(lines[idx+8],"dimension 2")
            self.assertEqual(lines[idx+10],"units real")
            self.assertEqual(lines[idx+12],"real")
            self.assertEqual(lines[idx+14],"2")

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