diff --git a/test/test_downsampled.py b/test/test_downsampled.py
index 062096181aa976173ec8966903dafc2a5eb15644..508bead16e76ffa888a09870c089b544615ce5c0 100644
--- a/test/test_downsampled.py
+++ b/test/test_downsampled.py
@@ -8,12 +8,17 @@ import numpy as np
 
 MAXINTERVAL = 65 #ms
 
-def get_max_acq(prefix, prefixdig, dec_fac):
+def get_max_acq(prefix, prefixdig, dec_fac, down_or_int="Dwn0"):
     fsamp = caget_assert(prefix + ":FreqSampling")
     neariqn = caget_assert(prefixdig + ":IQSmpNearIQ-N-RB")
 
     faclimit = 1/(fsamp*1000)*neariqn
-    return int(MAXINTERVAL/(faclimit*dec_fac))
+    nelm = caget_assert(prefixdig + (":%s-Cmp0.NELM") % (down_or_int))
+    # the limit is the maximum number of elements on waveform
+    if nelm < int(MAXINTERVAL/(faclimit*dec_fac)):
+        return nelm
+    else:
+        return int(MAXINTERVAL/(faclimit*dec_fac))
 
 def get_max_dec(prefix, prefixdig, smnm):
     fsamp = caget_assert(prefix + ":FreqSampling")
diff --git a/test/test_internal.py b/test/test_internal.py
index 5407cde942e334ef740df1270265cc8e3345dfa7..19d3e5d41877c5a445398c35a244d793396c5d07 100644
--- a/test/test_internal.py
+++ b/test/test_internal.py
@@ -20,7 +20,7 @@ class TestInternalChannels:
             if ch != "ILCtrl":
                 assert check_readback(prefixdig + (":IntCh%sDecF" % ch), 1)
             # number of samples
-            smnm = randint(1, get_max_acq(prefix, prefixdig, 1))
+            smnm = randint(1, get_max_acq(prefix, prefixdig, 1, ("IntCh%s" % ch)))
             caput_assert(prefixdig + (":IntCh%sSmpNm" % ch), smnm)
             sleep(0.1)
             smnm_rbv = caget_assert(prefixdig + (":IntCh%sSmpNm-RB" % ch))
@@ -41,11 +41,11 @@ class TestInternalChannels:
             # check limit for number of samples
             if ch != "ILCtrl":
                 assert check_readback(prefixdig + (":IntCh%sDecF" % ch), 1)
-            smnm = randint(get_max_acq(prefix, prefixdig, 1), 10*get_max_acq(prefix, prefixdig, 1))
+            smnm = randint(get_max_acq(prefix, prefixdig, 1, ("IntCh%s" % ch)), 10*get_max_acq(prefix, prefixdig, 1, ("IntCh%s" % ch)))
             caput_assert(prefixdig + (":IntCh%sSmpNm" % ch), smnm)
             sleep(0.1)
             smnm_rbv = caget_assert(prefixdig + (":IntCh%sSmpNm-RB" % ch))
-            assert smnm_rbv <= get_max_acq(prefix, prefixdig, 1)
+            assert smnm_rbv <= get_max_acq(prefix, prefixdig, 1, ("IntCh%s" % ch))
 
             #ILCtrl doesn't have decimation
             if ch != "ILCtrl":
@@ -65,7 +65,7 @@ class TestInternalChannels:
                 caput_assert(prefixdig + (":IntCh%sDecF" % ch), 1)
                 sleep(0.1)
 
-            caput_assert(prefixdig + (":IntCh%sSmpNm" % ch), get_max_acq(prefix, prefixdig, 1))
+            caput_assert(prefixdig + (":IntCh%sSmpNm" % ch), get_max_acq(prefix, prefixdig, 1, ("IntCh%s" % ch)))
             sleep(0.1)
 
 
@@ -117,7 +117,7 @@ class TestInternalChannels:
                 assert change_state(prefixdig, state)
             if ch != "ILCtrl":
                 assert check_readback(prefixdig + (":IntCh%sDecF" % ch), 1)
-            smnm = randint(1, get_max_acq(prefix, prefixdig, 1))
+            smnm = randint(1, get_max_acq(prefix, prefixdig, 1, ("IntCh%s" % ch)))
             caput_assert(prefixdig + (":IntCh%sSmpNm" % ch), smnm)
             sleep(0.1)
             smnm_rbv = caget_assert(prefixdig + (":IntCh%sSmpNm-RB" % ch))
diff --git a/test/test_pi_magang_tables.py b/test/test_pi_magang_tables.py
index 4b58ebc1f3c7e934da49a27355227690074d39cc..96487917a189d92b6c37da840e8ca09ae4ff9a8c 100644
--- a/test/test_pi_magang_tables.py
+++ b/test/test_pi_magang_tables.py
@@ -8,7 +8,8 @@ from helper import check_readback, change_state, sim_bp_trig, caget_assert, \
                    caput_assert,fixed_to_float, float_to_fixed
 import numpy as np
 
-MAXELEM = 677721 # 1 order less than the maximum size
+MAXELEM = 50000
+MAXCALELEM = 5000
 
 IQLIMITS = [-1, 0.999969] # for SP and FF
 QMNIQ = (1, 15, 1)
@@ -297,7 +298,7 @@ class TestPICalibTables:
         caput_assert(prefixdig+":RFCtrl" + pitype + "CalEn", 0)
 
         # generate RAW and EGU arrays
-        nelem = randint(20,MAXELEM)
+        nelem = randint(20,MAXCALELEM)
         slope = uniform(2, 100)
         offset = uniform(2, 100)
 
@@ -310,6 +311,7 @@ class TestPICalibTables:
         egu = []
         for i in range(nelem):
             egu.append(raw[i]*slope + offset)
+        egu = sorted(egu)
 
         caput_assert(prefixdig+":RFCtrl" + pitype + "CalEGU", egu)
         caput_assert(prefixdig+":RFCtrl" + pitype + "CalRaw", raw)